0

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

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
    }

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

4

2 回答 2

4

将您的方法更改为 returnActionResult而不是ViewResult. 后者期望您将返回View(),而前者是允许任何类型的 ActionResult 对象的基本类型,例如由RedirectToActionand返回的对象View

于 2012-04-20T12:28:31.693 回答
2
public ActionResult Index(){

}

代替

public ViewResult Index(){

}
于 2012-04-20T12:30:07.950 回答