对于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