對於MVC 5,可能還有4個,RouteConfig現在是默認路由定義的地方。默認路由映射之前,我有一些「routes.IgnoreRoute」來電,這裏是一個例子:
routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
現在,當我添加了一個設置區域的網站,導航到的URL會在該區域點擊一個動作,然後嘗試導航到change.account,我在RouteConfig中的忽略路由調用不再阻止該呼叫被饋送到路由引擎。
在閱讀本文並瀏覽其他地方之後,我意識到我基本上可以在SettingsAreaConfiguration.cs文件中使用相同的忽略路由調用,但路由作爲AreaRegistrationContext的屬性存在細微差別。
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });
context.MapRoute(
"Settings_default",
"Settings/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
哇哇!問題解決了!!
現在我對IgnoreRoute進行了很多調用,因此我將所有在我的站點中標準化的調用複製到擴展了RouteCollection的新擴展方法中。
public static class RouteCollectionExtensions
{
public static void IgnoreRoutesCommon(this RouteCollection routes)
{
routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" });
}
}
我可以添加更多的IgnoreRoute調用這個方法裏面,現在我有一個集中的地方來存儲所有這些調用。
現在,我可以使用對我的擴展方法的調用來替換「change.account」的特定IgnoreRoute調用。
所以在RouteConfig.cs文件調用看起來像:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoutesForCommon();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
而且在SettingsAreaConfiguration.cs文件調用看起來像:
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.IgnoreRoutesForCommon();
context.MapRoute(
"Settings_default",
"Settings/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
徐-weet! :O)
IgnoreRoute()用於你的Global.asax RegisterRoutes方法,它並不真正知道你忽略了某個區域,控制器或某個文件夾路徑的天氣。所有看到的都是網址。 – 2010-09-12 19:39:27
我有像你一樣的問題,我有兩個領域,它不會忽略區域管理員的路線! – Khaldoun 2011-11-23 18:50:30