有人可以向我解释asp.net路由语法吗?

时间:2023-02-09 01:35:52

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

我正在Web窗体场景中处理此代码:

  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:

首先,谁能告诉我{* pathInfo}的定义在哪里? http://msdn.microsoft.com/en-us/library/cc668201.aspx#url_patterns并没有真正定义它。请问:

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

第三,是否需要添加这些表达式以正确忽略路由?我知道{* url}是某种捕获,如果IgnoreRoutes在它之前或之后出现,例如

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

2 个解决方案

#1


23  

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,

我的2美分:路线不是正则表达式。它只是构成路径的变量和静态组件,由段(由斜杠标识)分隔。有一个特殊符号,即最后一个变量中的星号,这意味着从这里开始,忽略segment-separator - 斜杠。所以,

{*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.

是最简单的路由,因为它意味着获取整个URL,将其放入变量'url',并将其传递给与该路由关联的页面。

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

将所有内容放在第一个段 - 直到第一个斜杠 - 放入变量'controller',将所有内容放在第一个和第二个/变量'action'之间,以及第二个和第三个斜杠之间的所有内容(或结束)进入变量'id'。然后将这些变量传递到关联页面。

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

在这里,将.axd /(并且它不能有任何斜杠!)之前的信息放入'resource',并将第一个/之后的所有内容放入'pathInfo'。由于这通常是ignoreRoute,因此它不是将其传递给关联的页面,而是由Stop​​Handler处理,这意味着路由不会处理它,而是由非路由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.

正如bleevo所说,路线是按照它们被添加到集合中的顺序执行的。所以必须在处理通用路由之前添加IgnoreRoute。

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

这是马的嘴: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.

具体到你的例子,我会将IgnoreRoute线放在Route添加的上方,因为你的路线实际上是一个全能的。另外,请记住,只有当gif位于根目录中时才会遵守.gif忽略。

#2


2  

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

pathinfo只是一个桶的标签。因此,例如{* pathinfo}表示将{resource} .axd /之后的所有内容放入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.

路由按照您将它们放在路由表中的顺序执行,因此如果您的第一个路由是捕获,则所有其余路径将永远不会执行。

#1


23  

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,

我的2美分:路线不是正则表达式。它只是构成路径的变量和静态组件,由段(由斜杠标识)分隔。有一个特殊符号,即最后一个变量中的星号,这意味着从这里开始,忽略segment-separator - 斜杠。所以,

{*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.

是最简单的路由,因为它意味着获取整个URL,将其放入变量'url',并将其传递给与该路由关联的页面。

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

将所有内容放在第一个段 - 直到第一个斜杠 - 放入变量'controller',将所有内容放在第一个和第二个/变量'action'之间,以及第二个和第三个斜杠之间的所有内容(或结束)进入变量'id'。然后将这些变量传递到关联页面。

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

在这里,将.axd /(并且它不能有任何斜杠!)之前的信息放入'resource',并将第一个/之后的所有内容放入'pathInfo'。由于这通常是ignoreRoute,因此它不是将其传递给关联的页面,而是由Stop​​Handler处理,这意味着路由不会处理它,而是由非路由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.

正如bleevo所说,路线是按照它们被添加到集合中的顺序执行的。所以必须在处理通用路由之前添加IgnoreRoute。

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

这是马的嘴: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.

具体到你的例子,我会将IgnoreRoute线放在Route添加的上方,因为你的路线实际上是一个全能的。另外,请记住,只有当gif位于根目录中时才会遵守.gif忽略。

#2


2  

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

pathinfo只是一个桶的标签。因此,例如{* pathinfo}表示将{resource} .axd /之后的所有内容放入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.

路由按照您将它们放在路由表中的顺序执行,因此如果您的第一个路由是捕获,则所有其余路径将永远不会执行。