简体   繁体   English

这两个ASP.NET MVC IgnoreRoute指令有什么区别?

[英]What is the difference between these two ASP.NET MVC IgnoreRoute directives?

The default ASP.NET MVC 3 project template contains the following IgnoreRoute directive: 默认的ASP.NET MVC 3项目模板包含以下IgnoreRoute指令:

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

I have now seen multiple projects change this line (including StackExchange's DataExplorer ) to instead something that looks like: 我现在看到多个项目改变了这一行(包括StackExchange的DataExplorer ),而不是看起来像:

routes.IgnoreRoute("{*allaxd}", new {allaxd = @".*\.axd(/.*)?"});

Could anyone explain in what scenario or in general why the default .axd route ignoring wouldn't be adequate while this latter version would be? 任何人都可以解释在什么情况下或一般情况下为什么默认的.axd路由忽略不适合后者版本? Or the other way around, why might one choose not to use this latter version and instead just stick with the default? 或者反过来说为什么人们会选择不使用后一个版本而只是坚持默认?

I have to admit I don't fully understand the IgnoreRoute syntax, and the MSDN documentation on the subject is pretty terse. 我不得不承认我并不完全理解IgnoreRoute语法, 关于这个主题MSDN文档非常简洁。

There's an explanation in Phil Haack's blog: Make Routing Ignore Requests For A File Extension Phil Haack的博客中有一个解释: 使路由忽略文件扩展的请求

The basic idea, quoting Phil, is: 引用菲尔的基本想法是:

One solution to this is to add an appropriate ignore route to indicate that routing should ignore these requests. 对此的一个解决方案是添加适当的忽略路由以指示路由应忽略这些请求。 Unfortunately, we can't do something like this: 不幸的是,我们做不到这样的事情:

{*path}.aspx/{*pathinfo}

We only allow one catch-all route and it must happen at the end of the URL. 我们只允许一个catch-all路由,它必须发生在URL的末尾。 However, you can take the following approach.... 但是,您可以采取以下方法....

What I'm doing here is a technique Eilon showed me which is to map all URLs to these routes, but then restrict which routes to ignore via the constraints dictionary. 我在这里做的是Eilon向我展示的一种技术,即将所有URL映射到这些路由,然后通过约束字典限制忽略哪些路由。 So in this case, these routes will match (and thus ignore) all requests for favicon.ico (no matter which directory) as well as requests for a .aspx file. 因此,在这种情况下,这些路由将匹配(并因此忽略)对favicon.ico的所有请求(无论哪个目录)以及对.aspx文件的请求。 Since we told routing to ignore these requests, normal ASP.NET processing of these requests will occur. 由于我们告诉路由忽略这些请求,因此将发生这些请求的正常ASP.NET处理。

The difference between the two IgnoreRoute calls from the original post is the following: 来自原始帖子的两个IgnoreRoute调用之间的区别如下:

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

This will match requests to resources like /ScriptManager.axd or /Foo.axd/bar/baz.aspx , but will not match a request to any *.axd resource below the root of your site, like /foo/bar/Baz.axd . 这将匹配请求像/ScriptManager.axd/Foo.axd/bar/baz.aspx资源,但不会请求任何*个.axd资源匹配您的网站的根目录下,如/富/酒吧/巴兹。 axd

routes.IgnoreRoute("{*allaxd}", new {allaxd = @".*\.axd(/.*)?"});

This call uses a regular expression to match a request to any *.axd resource, at any level of your site, such as /foo/bar/Baz.axd . 此调用使用正则表达式将请求匹配到站点任何级别的任何* .axd资源,例如/foo/bar/Baz.axd Therefore, this call would be preferable to the first one if you reference any axd resources below the root of your site. 因此,如果您引用站点根目录下的任何axd资源,则此调用将优先于第一个调用。

If you break down the regular expression, the .* at the beginning will match 0 or more of any character. 如果分解正则表达式,则开头的.*将匹配任何字符的0或更多。 \\.axd will match the literal string ".axd" , and (/.*)? \\.axd将匹配文字字符串".axd"(/.*)? will optionally match a slash followed by 0 or more of any character. 可选地匹配斜杠后跟0或更多任何字符。

The * in the {*allaxd} URL pattern ensures that the entire path will be scanned, and not just one section of the path. *{*allaxd} URL模式确保了整个路径将被扫描,并且该路径的不只是一个部分。 allaxd is simply an arbtrary label given to the matched portion of the path, which in this case will be the entire path. allaxd只是一个给予路径匹配部分的arbtrary标签,在这种情况下将是整个路径。 This method of ignoring routes effectively allows you to ignore routes for specific file extensions. 这种忽略路由的方法有效地允许您忽略特定文件扩展名的路由。 You could easily copy this call and make a few changes in order to ignore routes for *.aspx, *.asmx, etc, etc. 您可以轻松复制此调用并进行一些更改,以忽略* .aspx,* .asmx等的路由。

For detailed documentation on routing, I highly recommend the following MSDN page . 有关路由的详细文档,我强烈推荐以下MSDN页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM