重定向到基于角色的操作不起作用。也许需要路由?
Redirect to action based on role not working. Maybe routing needed?
我有以下控制器
public class GlobalAdminController : Controller
{
// GET: GlobalAdmin
[AuthorizeUser(Roles = "admin")]
public ActionResult Index()
{
return View();
}
}
以及家庭控制器,它是应用程序的主要登录页面
public ActionResult Index()
{
if(User.IsInRole("admin"))
{
RedirectToAction("Index", "GlobalAdmin");
}
return View();
}
重定向到动作在调试器中执行
但是,索引操作本身并未在全局管理员上执行
不知道有没有更好的方法呢?也许通过路由?
您必须使用 return
向浏览器提供重定向信息 (url
)。浏览器将重定向到新位置。
public ActionResult Index()
{
if(User.IsInRole("admin"))
{
return RedirectToAction("Index", "GlobalAdmin");
}
return View();
}
路由的重点是提供一种从控制器访问操作的方法。您不是访问 GlobalAdmin/Index
,而是使用路由提供另一种方式,例如 admin/index
routes.MapRoute(
name: "Default",
url: "admin/{action}/{id}",
defaults: new { controller = "GlobalAdmin", action = "Index", id = UrlParameter.Optional } );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
我有以下控制器
public class GlobalAdminController : Controller
{
// GET: GlobalAdmin
[AuthorizeUser(Roles = "admin")]
public ActionResult Index()
{
return View();
}
}
以及家庭控制器,它是应用程序的主要登录页面
public ActionResult Index()
{
if(User.IsInRole("admin"))
{
RedirectToAction("Index", "GlobalAdmin");
}
return View();
}
重定向到动作在调试器中执行 但是,索引操作本身并未在全局管理员上执行
不知道有没有更好的方法呢?也许通过路由?
您必须使用 return
向浏览器提供重定向信息 (url
)。浏览器将重定向到新位置。
public ActionResult Index()
{
if(User.IsInRole("admin"))
{
return RedirectToAction("Index", "GlobalAdmin");
}
return View();
}
路由的重点是提供一种从控制器访问操作的方法。您不是访问 GlobalAdmin/Index
,而是使用路由提供另一种方式,例如 admin/index
routes.MapRoute(
name: "Default",
url: "admin/{action}/{id}",
defaults: new { controller = "GlobalAdmin", action = "Index", id = UrlParameter.Optional } );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );