Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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# 根据返回视图()的switch语句中的用户角色返回RedirectToAction();s_C#_Asp.net Mvc - Fatal编程技术网

C# 根据返回视图()的switch语句中的用户角色返回RedirectToAction();s

C# 根据返回视图()的switch语句中的用户角色返回RedirectToAction();s,c#,asp.net-mvc,C#,Asp.net Mvc,用户成功登录后,我希望页面根据用户角色重定向到合适的页面。我已经有了一些代码,允许我从登录凭据中检索和检查用户角色,但是switch语句不允许我使用if-else语句 它希望我返回一个视图(),但我总是被重定向到Home/Index视图。这是我的密码: switch (result) { case SignInStatus.Success: MigrateShoppingCart(model.Email);

用户成功登录后,我希望页面根据用户角色重定向到合适的页面。我已经有了一些代码,允许我从登录凭据中检索和检查用户角色,但是switch语句不允许我使用if-else语句


它希望我返回一个视图(),但我总是被重定向到
Home/Index
视图。这是我的密码:

        switch (result)
        {

            case SignInStatus.Success:
                MigrateShoppingCart(model.Email);
                //returnURL needs to be decoded
                string decodedUrl = "";
                if (!string.IsNullOrEmpty(returnUrl))
                    decodedUrl = Server.UrlDecode(returnUrl);

                if (Url.IsLocalUrl(decodedUrl))
                {
                    return Redirect(decodedUrl);
                }

                string id = UserManager.FindByEmail(model.Email).Id;
                IList<string> roleNames = UserManager.GetRoles(id);

                if (roleNames.Contains("Admin"))
                {
                    RedirectToAction("Index", "Category");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
                return View();
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
开关(结果)
{
案例标志状态成功:
MigrateShoppingCart(model.Email);
//返回URL需要解码
字符串decodedUrl=“”;
如果(!string.IsNullOrEmpty(returnUrl))
decodedUrl=Server.UrlDecode(返回URL);
if(Url.IsLocalUrl(decodedUrl))
{
返回重定向(decodedull);
}
string id=UserManager.FindByEmail(model.Email).id;
IList roleNames=UserManager.GetRoles(id);
if(roleNames.Contains(“Admin”))
{
重定向操作(“索引”、“类别”);
}
其他的
{
返回重定向到操作(“索引”、“主页”);
}
返回视图();
案例标志状态锁定输出:
返回视图(“锁定”);
案例标志状态。要求验证:
return RedirectToAction(“SendCode”,new{ReturnUrl=ReturnUrl,RememberMe=model.RememberMe});
案例信号状态故障:
违约:
AddModelError(“,”登录尝试无效“);
返回视图(模型);
}

这是我的代码中的一个小错误,因为我在第一个if情况下没有使用
return
单词。更改此代码:

if (roleNames.Contains("Admin"))
{
    RedirectToAction("Index", "Category");
}
else
{
    return RedirectToAction("Index", "Home");
}
return View();
case SignInStatus.LockedOut:
return View("Lockout");
为此:

if (roleNames.Contains("Admin"))
{
    return RedirectToAction("Index", "Category");
}
else
{
    return RedirectToAction("Index", "Home");
}

case SignInStatus.LockedOut:
return View("Lockout");

请注意,我还可以删除
return view()
code。

错误消息是什么?即使用户具有“Admin”角色,我也总是被重定向到
Home/Index
视图。在调试和登录为Admin时,是否可以在包含“Admin”的rolenames值之前共享rolenames值。请看下面我的答案,这是我代码中的一个小问题。我应该在第一个if案例正文中使用
return
关键字。然后它允许我删除
返回视图()
行代码。