Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net核心登录函数错误“;并非所有代码路径都返回一个值";_C#_Asp.net Core_Controller - Fatal编程技术网

C# asp.net核心登录函数错误“;并非所有代码路径都返回一个值";

C# asp.net核心登录函数错误“;并非所有代码路径都返回一个值";,c#,asp.net-core,controller,C#,Asp.net Core,Controller,大家好,我正在尝试使这个登录功能。Login()方法出错,并非所有代码路径都返回值 我的行动方法如下: [HttpPost] public async Task<IActionResult> Login(LoginViewModel Input) { if (ModelState.IsValid) { var result = await signInManager.Pas

大家好,我正在尝试使这个登录功能。Login()方法出错,
并非所有代码路径都返回值

我的行动方法如下:

[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");
                }
            }
        }
[HttpPost]
公共异步任务登录(LoginViewModel输入)
{
if(ModelState.IsValid)
{
var result=await-signInManager.PasswordSignInAsync(Input.Email、Input.Password、Input.RememberMe、lockoutOnFailure:true);
if(result.successed)
{
logger.LogInformation(“用户登录”);
返回重定向到操作(“索引”、“主页”);
}
其他的
{
AddModelError(string.Empty,“登录尝试无效”);
返回重定向操作(“登录”、“用户”);
}
}
}
我添加了2个返回语句。我找不到我的错误。你能帮我解决这个问题吗


谢谢。

这里有两个
if
语句。其中一个语句还有一个else。因此,总共有三种可能的执行路径。如果第一个
If
语句不正确,那么它将完全跳过下一个包含所有返回路径的语句。因此,它到达函数的底部时没有返回值

试试这个

    [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");
    }
[HttpPost]
公共异步任务登录(LoginViewModel输入)
{
if(ModelState.IsValid)
{
var result=await-signInManager.PasswordSignInAsync(Input.Email、Input.Password、Input.RememberMe、lockoutOnFailure:true);
if(result.successed)
{
logger.LogInformation(“用户登录”);
//此处登录成功,因此我们现在返回,执行停止,这意味着底部代码永远不会运行。
返回重定向到操作(“索引”、“主页”);
}
}
//如果我们到达这一行,要么MoxelState无效,要么登录失败。
AddModelError(string.Empty,“登录尝试无效”);
返回重定向操作(“登录”、“用户”);
}

首先,if语句检查模型状态验证,但是您完全忽略了验证的下降,并且缺少此函数的返回。您应该在第一个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);
        }

公共异步任务登录(LoginViewModel输入)
{
if(ModelState.IsValid)
{
var result=await-signInManager.PasswordSignInAsync(Input.Email、Input.Password、Input.RememberMe、lockoutOnFailure:true);
if(result.successed)
{
logger.LogInformation(“用户登录”);
返回重定向到操作(“索引”、“主页”);
}
其他的
{
AddModelError(string.Empty,“登录尝试无效”);
返回重定向操作(“登录”、“用户”);
}
}
//你必须在这里归还一些东西
//返回视图(输入);
}

ModelState
是除
IsValid
之外的任何东西时会发生什么?不客气,哈哈。我一定是疯了。非常感谢。