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