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

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

问题描述

我处理在Web这个code窗体的情景:

 公共静态无效的RegisterRoutes(RouteCollection路线)
  {    路线R =新干线({* URL},新MyRouteHandler());
    routes.Add(R);
    routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
    routes.IgnoreRoute({}资源.gif注意/ {*} PATHINFO);  }

首先,谁能告诉我在哪里的{*} PATHINFO把定义是什么?
<一href=\"http://msdn.microsoft.com/en-us/library/cc668201.aspx#url_patterns\">http://msdn.microsoft.com/en-us/library/cc668201.aspx#url_patterns并没有真正定义它。请问:

  routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);

匹配

  /c/xyz.axd和
/b/c/xyz.axd和
/a/b/c/xyz.axd

  routes.IgnoreRoute({}资源个.axd);

只匹配

  /xyz.axd

其次,在

  {* URL}

这是什么意思*?又是什么除权pression作为一个整体平均值。有没有什么地方这是解释清楚?

第三,是否有我需要添加这些前pressions正确忽略路线特定的顺序?我知道{* URL}是某种包罗万象的,应在IgnoreRoutes来之前或之后如

  routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
routes.IgnoreRoute({}资源.gif注意/ {*} PATHINFO);
路线R =新干线({* URL},新MyRouteHandler());
routes.Add(R);


解决方案

我的2美分:
路由是不是正则表达式。这只不过是可变的,静态的组件,使路线,由段分开(由斜杠标识)。还有一个特殊符号,在最后一个变量星号,从这里意味着无视段分隔符 - 斜线。因此,

  {* URL}

是最简单的路线,因为这意味着需要对整个URL,把它放到变量网址,并传递到与该路由相关的页面。

  {控制器} / {行动} / {ID}

把一切都在第一片段 - 至第一斜线 - 入变量控制器,使在第二和第三斜线之间的第一和第二/入变量'动作',和一切之间的所有内容(或结束)到变量ID。然后将这些变量传递到相关页面。

  {}资源个.axd / {*} PATHINFO

这里,把信息之前个.axd /(和它不能有任何的斜线!)成资源,并经过所述第一/成'PATHINFO'把一切。由于这是一种典型的ignoreRoute,所以不是将它传递给相关联的网页,它是由Stop​​Handler,这意味着路由将不会处理它处理,并且它不是由非路由的HttpHandler处理。

由于bleevo说,路线按顺序执行它们添加到集合。所以IgnoreRoute S上的通用路由处理之前已经无以复加。

下面是马的嘴:<一href=\"http://msdn.microsoft.com/en-us/library/cc668201.aspx\">http://msdn.microsoft.com/en-us/library/cc668201.aspx

具体到你的榜样,我会把IgnoreRoute行路线除了上面的,因为你的路线实际上是一个包罗万象的。此外,请记住将.gif文件忽略才会兑现,如果GIF是在根目录。

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

解决方案

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.

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

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