繁体   English   中英

如何重定向具有错误角色的用户(已登录)以查看页面?

[英]How do I redirect a user (who is already logged in) with the incorrect role to view a page?

我想将角色不正确的用户发送到另一个页面,但是当我使用时:

return RedirectToAction("Index", "Home");

我得到错误:

Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.ViewResult'

这是因为我使用的是http GET而不是http POST吗?

这是我的代码:

public ViewResult Index()
{
    if (User.IsInRole("Administrator") | User.IsInRole("SuperAdministrator"))
    {
        //Do Admin Things
        return View()
    }
    else
    {
        // Send to a different page
        return RedirectToAction("Index", "Home"); // I want to do this, but it gives me an error
    }

在这种情况下,如何根据用户角色重定向用户?

更改您的方法以返回ActionResult而不是ViewResult 后者希望您将返回View() ,而前者是一个基本类型,它将允许任何类型的ActionResult对象,例如RedirectToActionView返回的对象

public ActionResult Index(){

}

代替

public ViewResult Index(){

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM