开发者

Can someone explain asp.net routing syntax to me?

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.Ad开发者_如何学Cd(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);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜