
[英]How to redirect already logged on user who is trying to access Logon page MVC3
[英]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对象,例如RedirectToAction
和View
返回的对象
public ActionResult Index(){
}
代替
public ViewResult Index(){
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.