2010-09-12 73 views
4

如何忽略区域内的路由?Asp.Net MVC IgnoreRoute在区域内

我在我的MVC应用程序中有captcha。它从GenericHanlder(.ashx)加载它的图像,所以如果imnot使用区域,它工作正常,如果我忽略来自该URL的路由。

即: 在Global.asax中

routes.IgnoreRoute(Counts/{filename}.{ashx}/{*pathinfo});//Counts is the controller name without an area 

的问题是,最近我将文件迁移到不同的区域和路线的路径被忽略了modified.Now其:

Admin/Acounts/Users/captcha.ashx?guid=3565465689789hfghfdf 

所以我试图改变在Global.asax中的routes.IgnoreRoutes方法路径:

routes.IgnoreRoute(Admin/Acounts/Users/{filename}.ashx/{*pathinfo}); 

但它不工作anymore.Ive已经尝试忽略RegisterArea方法途径,在AreaRegistrationFile:

context.Routes.IgnoreRoute("Admin/Accounts/Users/{filename}.ashx/{*pathinfo}"); 

但也doenst工作。

任何想法如何忽略路线区?

+0

IgnoreRoute()用于你的Global.asax RegisterRoutes方法,它并不真正知道你忽略了某个区域,控制器或某个文件夹路径的天气。所有看到的都是网址。 – 2010-09-12 19:39:27

+0

我有像你一样的问题,我有两个领域,它不会忽略区域管理员的路线! – Khaldoun 2011-11-23 18:50:30

回答

4

IgnoreRoute实际上只是对Add(new Route())的调用,Route集的RouteHandler参数是一个新的StopRoutingHandler。 StopRoutingHandler通知URL路由模块忽略路由并获取下一个内联HttpHandler。

你知道路由注册对声明的顺序很敏感。我的猜测是,在一些其他路由已经捕获它之后,你正在声明你的IgnoreRoute。

如果这一点信息不能帮助你,请张贴你的路线注册的全部内容,因为它们将帮助我们给你答案。

此外,如果您使用http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx提供的源代码来调试路由,找到问题的原因将更加容易。

3

这对我的作品在AreaRegistration类面积:

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.Routes.Add(new Route("admin/{sub}/{resource}.axd/{*pathInfo}", new StopRoutingHandler())); 
    context.Routes.Add(new Route("admin/{resource}.axd/{*pathInfo}", new StopRoutingHandler())); 

    context.MapRoute(
     "admin_default", 
     "admin/{controller}/{action}/{id}", 
     new { controller = "home", action = "Index", id = UrlParameter.Optional } 
    ); 
} 

这是MVC4但它可能在旧版本正常工作。

+0

非常感谢。这正是我所需要的,因为在RouteConfig中直接忽略路由对我不起作用,而'context.IgnoreRoute()'在'RegisterArea'方法中不是有效的方法调用。 – silkfire 2015-09-19 18:07:10

1

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