Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 350
ckittel Avatar asked Aug 22 '11 19:08

ckittel


Video Answer


1 Answers

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.

like image 195
Craig Stuntz Avatar answered Sep 24 '22 14:09

Craig Stuntz