Asp.net mvc ASP.NET MVC-取决于角色的多个默认索引页
是否可以通过路由执行基于角色的重定向? 我的目标是根据用户的角色将其重定向到主页,而不是在登录操作帖子中使用RedirectToAction来增强网站的响应能力 我想要这样的东西Asp.net mvc ASP.NET MVC-取决于角色的多个默认索引页,asp.net-mvc,roles,Asp.net Mvc,Roles,是否可以通过路由执行基于角色的重定向? 我的目标是根据用户的角色将其重定向到主页,而不是在登录操作帖子中使用RedirectToAction来增强网站的响应能力 我想要这样的东西 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); if (User.IsInRole("Admin"
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
if (User.IsInRole("Admin"))
{
routes.MapRoute(
"Admin", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "User", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
else
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
}
路由是在应用程序启动时设置的,因此路由是在对用户进行身份验证之前建立的。您可以使用通用操作方法:
public ActionResult Go()
{
if (User.IsInRole("Admin")) {
return RedirectToAction("List", "User");
}
else {
return RedirectToAction("Index", "Home");
}
}
将所有用户导航到此Go action方法,它会将他们重定向到适当的位置。如果您希望两个案例的路径相同,但页面的内容不同,也可以返回不同的视图:
这不是组织视图的最佳方式,但这样的用例会发生,这就是您可以轻松实现它们的方式。不,这是不可能的。是什么让你认为这会提高反应能力?谢谢你的回答。我知道一般的行动方法。我在另一个帖子里找到的。问题是,这篇文章的最后一个活动是大约3年前的2.NET版本,所以我想确保这个解决方案仍然有效。
public ActionResult GetStartPage()
{
// ...
if (User.IsInRole("Admin"))
{
return View("~/Views/Home/List.cshtml", viewModel);
}
else
{
return View("~/Views/Home/Index.cshtml", viewModel);
}
}