26

I am dealing with this code in a Web Forms scenario:

  public static void RegisterRoutes(RouteCollection routes)
  {

    Route r = new Route("{*url}", new MyRouteHandler());
    routes.Add(r);
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{resource}.gif/{*pathInfo}");

  }

Firstly, can anyone tell me where the defintion of {*pathInfo} is? http://msdn.microsoft.com/en-us/library/cc668201.aspx#url_patterns doesn't really define it. Does:

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

Match

/c/xyz.axd and 
/b/c/xyz.axd and
/a/b/c/xyz.axd 

Whereas

routes.IgnoreRoute("{resource}.axd");

Only matches

/xyz.axd

Secondly, in:

{*url}

What does * mean? And what does the expression as a whole mean. Is there somewhere this is clearly explained?

Thirdly, is there a particular order I need to add these expressions to correctly ignore routes? I know {*url} is some kind of catchall, should the IgnoreRoutes come before or after it eg

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.gif/{*pathInfo}");
Route r = new Route("{*url}", new MyRouteHandler());
routes.Add(r);

2 Answers 2

25

My 2 cents: A route is not regex. It is simply variable and static components that make up a route, separated by segments (identified by a slash). There's one special symbol, the asterisk in the last variable, which means from here on, ignore the segment-separator -- the slash. So,

{*url} 

is the simplest route, because it means take the entire URL, put it into the variable 'url', and pass that to the page associated with that route.

{controller}/{action}/{id}

puts everything in the first segment -- up to the first slash -- into the variable 'controller', puts everything between the first and second / into the variable 'action', and everything between the second and third slash (or the end) into the variable 'id'. those variables are then passed into the associated page.

{resource}.axd/{*pathInfo}

here, put the info before .axd/ (and it can't have any slashes!) into 'resource', and put everything after the first / into 'pathInfo'. Since this is typically an ignoreRoute, so instead of passing it to the page associated, it is handled by the StopHandler, which means that routing won't deal with it, and it is instead handled by the non-routing HttpHandler.

As bleevo says, routes are executed in order they're added to the collection. so IgnoreRoute s have to be added before the generic route is handled.

Here's the horse's mouth: http://msdn.microsoft.com/en-us/library/cc668201.aspx

Specific to your example, I would put the IgnoreRoute lines above your Route addition, because your route is effectively a catch-all. Also, remember that the .gif ignore will only be honoured if the gif is in the root directory.

2

pathinfo is just a label for a bucket. So for instance {*pathinfo} says put everything after {resource}.axd/ into pathinfo.

The routes are executing in the order you place them in the routes table, so if your very first route is a catch all the rest will never execute.

2
  • Is my example of pathInfo above correct? If not, can you redo it so it is correct. I am still confused.
    – Petras
    Jul 18, 2010 at 12:41
  • Your examples look fine, maybe you could post a route thats not working, then I can comment on that.
    – bleevo
    Jul 21, 2010 at 10:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.