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?
Published Thursday, November 11, 2010 5:15 PM by pjohnson
Filed under: ,

Comments

# re: MVC's IgnoreRoute syntax

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...)

Wednesday, December 1, 2010 9:17 AM by Arjan Einbu

# re: MVC's IgnoreRoute syntax

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.

Tuesday, October 8, 2013 12:50 AM by bgillingham

# re: MVC's IgnoreRoute syntax

This is certainly again a coat design and style but with filled outlooks and insulated to make you heat within the severe weather conditions.

Sunday, December 1, 2013 2:57 PM by north face jackets on sale for cyber monday

Leave a Comment

(required) 
(required) 
(optional)
(required)