2010-09-12 72 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)