What is the difference between these two ASP.NET MVC IgnoreRoute directives?

The default ASP.NET MVC project template contains the following IgnoreRoute directive:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

Now I saw how several projects change this line (including StackExchange DataExplorer ) instead of what looks like this:

 routes.IgnoreRoute("{*allaxd}", new {allaxd = @".*\.axd(/.*)?"}); 

Can someone explain in what scenario or even why ignoring the default .axd route will not be adequate while this latest version is there? Or vice versa, why you can choose not to use this latest version and instead just stick to the default value?

I must admit that I do not quite understand the IgnoreRoute syntax, and the MSDN documentation on the topic is pretty brief.

+7
source share
2 answers

Phil Haack Blog Explains: Routing Missed Requests to File Extension

The main idea quoted by Phil is this:

One solution to this is to add an appropriate ignore route, indicating that routing should ignore these requests. Unfortunately, we cannot do something like this:

 {*path}.aspx/{*pathinfo} 

We allow only one route for all, and this should happen at the end of the URL. However, you can take the following approach ...

What I'm doing here is a method that showed me Eilon, which should display all the URLs of these routes, but then limit which routes to ignore through the restriction dictionary. Thus, in this case, these routes will match (and thus ignore) all requests for favicon.ico (no matter which directory), as well as requests for the .aspx file. Since we said that routing ignores these requests, the normal processing of these ASP.NET requests will be performed.

+6
source

The difference between the two IgnoreRoute calls from the original message is as follows:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

This will match resource requests such as /ScriptManager.axd or /Foo.axd/bar/baz.aspx , but will not match the request with any resource *. axd under the root of your site, for example /foo/bar/Baz.axd .

 routes.IgnoreRoute("{*allaxd}", new {allaxd = @".*\.axd(/.*)?"}); 

This call uses a regular expression to match the request to any resource *. axd at any level of your site, for example /foo/bar/Baz.axd . Therefore, this call would be preferable to the first if you link to any axd resources below the root of your site.

If you break the regular expression,. .* At the beginning will match 0 or more characters. \.axd will match the literal string ".axd" , and (/.*)? will not necessarily match a slash followed by 0 or more characters.

* in the URL pattern, {*allaxd} ensures that the entire path is {*allaxd} , not just one section of the path. allaxd is just an arbitrary label given to the agreed part of the path, which in this case will be the whole path. This method of ignoring routes effectively allows you to ignore routes for specific file extensions. You can easily copy this call and make a few changes to ignore routes for * .aspx, * .asmx, etc. Etc.

For detailed routing documentation, I highly recommend the following MSDN page .

+3
source

All Articles