MVC's IgnoreRoute syntax

I've had an excuse to mess around with custom route ignoring code in ASP.NET MVC, and am surprised how poorly the IgnoreRoute extension method on RouteCollection (technically RouteCollectionExtensions, but also RouteCollection.Add, and RouteCollection.Ignore which was added in .NET 4) is documented, both in the official docs by Microsoft, and various bloggers and forum participants who have been using it, some for years.

We all know these work. The first is in Microsoft code; it and the second are about the only examples out there:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Ignore("{*allaspx}", new {allaspx=@".*\.aspx(/.*)?"});

I understand the first ignores .axd requests regardless of what comes after the .axd part, and the second uses a regex custom constraint for more control over what's blocked. But why is pathInfo a special name that we don't have to define? What other special names could we use without defining? What does .* mean in a regex, if that really is a regex? . is exactly one instance, and * is zero or more instances, so putting them together doesn't make sense to me.

Phil Haack provides this equally syntactically confusing example to prevent favicon.ico requests from going through routing:

routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});

I also tried these in my code:

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

The first with URL's that ended in /myroute, and /myroute/whatever, but not /myroute/whatever?stuff=23. The second blocked all three of those, but not /somethingelse?stuff=myroute. Why does this work without putting the constant "myroute" in {} like the resource.axd example above? Is resource really a constant in that example, or just a placeholder for which we could have used any name? Do string constants need curly brace delimiters in some cases and not others? An example I found on Steve Smith's blog and on others shows the same thing:

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

This prevents any requests to the standard Content folder from going through routing. Why the curly braces?

Just to keep things interesting, when I tried to type my IgnoreRoute call above, I accidentally left out the slash:

routes.IgnoreRoute("myroute{*pathInfo}");

which threw "System.ArgumentException: A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter." OK, so no slash means more than one section, and including a slash means it's only one section?

Has anyone else had more luck using this method, or found any way to go about it other than trial and error?

2 Comments

  • Here are two answers for you:

    1.) In RegEx, the . (dot) means "any character" and not neccesarily exactly once. The * after the . is the quantifier (ie. how many?). So .* means zero or more of any character. (Its just a placeholder for whatever...)

    2.) You question about the "special name 'pathInfo' that we don't have to define" in the routes.IgnoreRoute("{resource}.axd/{*pathInfo}") sample:
    This is a catch-all route parameter that will catch the remains of the URL. To do this, the ending route element name is prepended with an asterisk. So as you can see, there's nothing special about the name 'pathInfo'.

    I agree on the documentation not being very informative, as I needed to look up the differences between Ignore and IgnoreRoutes and the both are described seperatly as if the other didn't exists. (Thats how I found your post...)

  • I'd like to see real documentation too... but isn't MVC open source? At least, if someone is desperate, they can maybe go to the source.

Comments have been disabled for this content.