asp.net核心登录功能错误"not all code paths return a value"
asp.net core Login function error "not all code paths return a value"
大家好,我正在尝试制作这个登录功能。我在 Login() 方法上遇到错误,not all code paths return a value
。
我的操作方法如下:
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel Input)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
logger.LogInformation("User logged in.");
return RedirectToAction("index", "home");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return RedirectToAction("Login", "User");
}
}
}
我添加了 2 个 return 语句。我找不到我的错误。你能帮我解决这个问题吗?
谢谢。
有两个if
语句。其中一个语句也有一个 else。所以总共有三种可能的执行路径。如果第一个 if
语句不正确,那么它会完全跳过下一个具有所有 return 路径的语句。因此它在没有 return 值的情况下到达函数的底部。
试试这个。
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel Input)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
logger.LogInformation("User logged in.");
// Login is successful here, so we return now and the execution stops, meaning the bottom code never runs.
return RedirectToAction("index", "home");
}
}
// If we get to this line, either the MoxelState isn't valid or the login failed.
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return RedirectToAction("Login", "User");
}
首先 if 语句检查模型状态验证,但您完全忽略了验证的失败并且缺少此函数的 return。您应该在第一个 modelstate.isvalid if 语句之后添加 return view() 或适合您情况的任何内容。我添加为评论。
public async Task<IActionResult> Login(LoginViewModel Input)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
logger.LogInformation("User logged in.");
return RedirectToAction("index", "home");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return RedirectToAction("Login", "User");
}
}
//you must return something here
//return View(Input);
}
大家好,我正在尝试制作这个登录功能。我在 Login() 方法上遇到错误,not all code paths return a value
。
我的操作方法如下:
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel Input)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
logger.LogInformation("User logged in.");
return RedirectToAction("index", "home");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return RedirectToAction("Login", "User");
}
}
}
我添加了 2 个 return 语句。我找不到我的错误。你能帮我解决这个问题吗?
谢谢。
有两个if
语句。其中一个语句也有一个 else。所以总共有三种可能的执行路径。如果第一个 if
语句不正确,那么它会完全跳过下一个具有所有 return 路径的语句。因此它在没有 return 值的情况下到达函数的底部。
试试这个。
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel Input)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
logger.LogInformation("User logged in.");
// Login is successful here, so we return now and the execution stops, meaning the bottom code never runs.
return RedirectToAction("index", "home");
}
}
// If we get to this line, either the MoxelState isn't valid or the login failed.
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return RedirectToAction("Login", "User");
}
首先 if 语句检查模型状态验证,但您完全忽略了验证的失败并且缺少此函数的 return。您应该在第一个 modelstate.isvalid if 语句之后添加 return view() 或适合您情况的任何内容。我添加为评论。
public async Task<IActionResult> Login(LoginViewModel Input)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
logger.LogInformation("User logged in.");
return RedirectToAction("index", "home");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return RedirectToAction("Login", "User");
}
}
//you must return something here
//return View(Input);
}