有人可以向我解释 asp.net 路由语法吗? [英] Can someone explain asp.net routing syntax to me?

查看:24
本文介绍了有人可以向我解释 asp.net 路由语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Web 表单场景中处理此代码:

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}");

  }

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

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}");

匹配

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

然而

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

只匹配

/xyz.axd

其次,在:

{*url}

* 是什么意思?整个表达是什么意思.有什么地方解释清楚吗?

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

第三,我需要添加这些表达式以正确忽略路由的特定顺序吗?我知道 {*url} 是某种笼统的东西,如果 IgnoreRoutes 出现在它之前或之后,例如

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 分钱:路由不是正则表达式.它只是组成路由的可变和静态组件,由段分隔(由斜线标识).有一个特殊符号,最后一个变量中的星号,这意味着从这里开始,忽略段分隔符——斜线.所以,

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} 

是最简单的路由,因为这意味着获取整个 URL,将其放入变量 '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}

将第一段中的所有内容——直到第一个斜杠——放入变量控制器"中,将第一个和第二个/之间的所有内容放入变量动作"中,以及第二个和第三个斜杠之间的所有内容(或最后)到变量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}

在这里,将 .axd/之前的信息(它不能有任何斜线!)放入资源"中,并将第一个/之后的所有内容放入路径信息"中.由于这通常是一个ignoreRoute,所以它不是传递给关联的页面,而是由Stop​​Handler处理,这意味着路由不会处理它,而是由非路由HttpHandler处理.

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.

正如 bleevo 所说,路由是按照添加到集合中的顺序执行的.因此必须在处理通用路由之前添加 IgnoreRoute .

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.

这是马的嘴:http://msdn.microsoft.com/en-us/library/cc668201.aspx

具体到您的示例,我会将 IgnoreRoute 行放在您的 Route 添加之上,因为您的路线实际上是一个包罗万象的路线.另外,请记住,仅当 gif 位于根目录中时才会忽略 .gif.

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.

这篇关于有人可以向我解释 asp.net 路由语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆