Asp.Net MVC IgnoreRoute Inside Area

How to ignore the route inside the area?

I have a captcha in my MVC application. It loads its image from GenericHanlder (.ashx), so if imnot uses regions, it works fine if I just ignore routes from this URL.

i.e.: In Global.asax

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

The problem is that recently I moved files to another area and the path to the path to be ignored has been changed. Now his:

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

So, I'm trying to change this path in routes. IgnoreRoutes method in Global.asax:

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

But it doesn't work anymore. I am already trying to ignore this path in the RegisterArea method, in AreaRegistrationFile:

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

But it also does the job.

Any ideas on how to ignore area routing?

+4
source share
3 answers

IgnoreRoute is just a call to Add (new route ()), with the RouteHandler parameter for the Route set being the new StopRoutingHandler. StopRoutingHandler tells the URL routing module to ignore the route and get everything that comes next in the HttpHandler line.

You know that route registration is sensitive to the order of the declaration. I assume that you declare your IgnoreRoute after some other route has already caught it.

If this bit of information does not help you, post the full contents of your route registration, as they will help us give you answers.

Also, if you use the source code provided by http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx to debug routes, it will be much easier for you to find the cause of your problem.

+4
source

This works for me in the AreaRegistration class of an area:

 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 } ); } 

This is in MVC4, but it may work in older versions.

+3
source

For MVC 5, probably 4 as well, RouteConfig is now where the default route is defined. Before matching the default routes, I had several calls to "routes.IgnoreRoute", and here is one example:

 routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

Now, when I added the settings area to the site, navigated to the URL that would hit the action in that area, and then tried to switch to change.account, my ignore route call in RouteConfig no longer blocked this call from being submitted to the routing mechanism.

After reading this post and looking at other places, I realized that I can essentially have the same route as in the SettingsAreaConfiguration.cs file, with the slight difference that the Routes live as the AreaRegistrationContext property.

 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 } ); } 

And wow! The problem is solved!

Now I had quite a few IgnoreRoute calls, so I copied all those calls that were standard on my site to a new extension method that extends RouteCollection.

 public static class RouteCollectionExtensions { public static void IgnoreRoutesCommon(this RouteCollection routes) { routes.IgnoreRoute("{*changeaccount}", new { changeaccount = @"(.*/)?change.account(/.*)?" }); } } 

I can add more IgnoreRoute calls inside this method, now that I have a central place to store all these calls.

Now I can replace the specific IgnoreRoute call for "change.account" with a call to my extension method.

Thus, the call in the RouteConfig.cs file will look like this:

 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 } ); } 

And the call in the SettingsAreaConfiguration.cs file will look like this:

 public override void RegisterArea(AreaRegistrationContext context) { context.Routes.IgnoreRoutesForCommon(); context.MapRoute( "Settings_default", "Settings/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } 

Suh-Weet !: O)

+1
source

All Articles