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

1.3k views Asked by At

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

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

I have now seen multiple projects change this line (including StackExchange's DataExplorer) to instead something that looks like:

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

Could anyone explain in what scenario or in general why the default .axd route ignoring wouldn't be adequate while this latter version would be? Or the other way around, why might one choose not to use this latter version and instead just stick with the default?

I have to admit I don't fully understand the IgnoreRoute syntax, and the MSDN documentation on the subject is pretty terse.

2

There are 2 answers

0
Craig Stuntz On BEST ANSWER

There's an explanation in Phil Haack's blog: Make Routing Ignore Requests For A File Extension

The basic idea, quoting Phil, is:

One solution to this is to add an appropriate ignore route to indicate that routing should ignore these requests. Unfortunately, we can’t do something like this:

{*path}.aspx/{*pathinfo}

We only allow one catch-all route and it must happen at the end of the URL. However, you can take the following approach....

What I’m doing here is a technique Eilon showed me which is to map all URLs to these routes, but then restrict which routes to ignore via the constraints dictionary. So in this case, these routes will match (and thus ignore) all requests for favicon.ico (no matter which directory) as well as requests for a .aspx file. Since we told routing to ignore these requests, normal ASP.NET processing of these requests will occur.

0
Justin Holzer On

The difference between the two IgnoreRoute calls from the original post is the following:

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

This will match requests to resources like /ScriptManager.axd or /Foo.axd/bar/baz.aspx, but will not match a request to any *.axd resource below the root of your site, like /foo/bar/Baz.axd.

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

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

If you break down the regular expression, the .* at the beginning will match 0 or more of any character. \.axd will match the literal string ".axd", and (/.*)? will optionally match a slash followed by 0 or more of any character.

The * in the {*allaxd} URL pattern ensures that the entire path will be scanned, and not just one section of the path. allaxd is simply an arbtrary label given to the matched portion of the path, which in this case will be the entire path. This method of ignoring routes effectively allows you to ignore routes for specific file extensions. You could easily copy this call and make a few changes in order to ignore routes for *.aspx, *.asmx, etc, etc.

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