2
我想创建一个在ASP.NET MVC 5项目中具有常量路径的自定义URL路由。 例如,我想拥有“www.mysite/admin/controller/action/”admin
是一个常数。此外我还有一些路线。创建将管理部分与网站其他部分分开的自定义URL路由
之后,我想定义一个政策,在浏览器中输入admin/controller/action/
,定向到管理面板,否则如果admin/
不存在于URL中,则定向到常规页面。 为了实现这个目标,我在_ViewStart.cshtml
中编写了一些代码,但需要进行一些改进。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Tag",
url: "Tags/{tag}/{page}/{id}",
defaults: new { controller = "Article", action = "Index", tag = (string)null, id = UrlParameter.Optional, page = @"/d" }
);
routes.MapRoute(
name: "Tags",
url: "Tags/",
defaults: new { controller = "Tag", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
null,
"Page{page}",
new { Controller = "Article", action = "Index" },
new { page = @"/d" }
);
}
_ViewStart.cshtml:
@{
if (HttpContext.Current.User.IsInRole("Administrator"))
{
// ??? need some codes for directing just to the /admin part
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
非常感谢。为了管理布局,我使用了@Darin Dimitrov的解决方案。 http://stackoverflow.com/a/5161384/1817640 – Jahan 2014-11-07 18:32:25