了解 Global.asax 中的路由 (asp.net-mvc)

发布于 2024-07-30 00:34:42 字数 112 浏览 5 评论 0原文

在 Global.asax 中以下含义是什么?

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

In Global.asax what does the following signify?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心的位置 2024-08-06 00:34:42

这是学习 MVC 时真正令人沮丧的事情之一 - 此功能的文档非常糟糕 - 几乎没有任何内容: http://msdn.microsoft.com/en-us/library/dd470170(VS.100).aspx

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

这允许所有的something.axd文件在MVC之外运行——最后的“{*pathInfo}”允许忽略查询字符串(它是一种通配符)。

请注意,这不会将任何此类通配符应用于路径,因此:

trace.axd?clear=1 //excluded from MVC

mySubFolder/customResource.axd //MVC passed to mySubFolderController.customResource()

有帮助。 除了“{resource}”和“{*pathInfo}”之外,我无法找到任何像样的文档来说明什么是支持的关键字,什么是不支持的关键字,

但是有一个几乎完全未记录的功能,可以为您提供更多的控制权在这些被忽略的路由上:

//ignore all WebForms .aspx/.asmx/.ashx calls anywhere
routes.IgnoreRoute( "{*allaspx}", new { allaspx = @".*\.as[pmh]x(/.*)?" } );

如果您传递带有属性的未初始化对象,则该属性将成为您可以在路由中使用的关键字。

您不能在路线中传递正则表达式,但可以在此匿名属性中传递。

This is one of the really frustrating things about learning MVC - the documentation for this feature is awful - there's just hardly anything there: http://msdn.microsoft.com/en-us/library/dd470170(VS.100).aspx.

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

This allows all the something.axd files to run outside of MVC - that "{*pathInfo}" at the end allows query strings to be ignored (it's kind of a wildcard).

Note that this doesn't apply any such wildcard to the path, so:

trace.axd?clear=1 //excluded from MVC

mySubFolder/customResource.axd //MVC passed to mySubFolderController.customResource()

Helpful. I've been unable to find any decent documentation on exactly what is and isn't supported as keywords apart from "{resource}" and "{*pathInfo}"

However there is an almost completely undocumented feature that gives you a lot more control over these ignored routes:

//ignore all WebForms .aspx/.asmx/.ashx calls anywhere
routes.IgnoreRoute( "{*allaspx}", new { allaspx = @".*\.as[pmh]x(/.*)?" } );

If you pass an anon-initialised object with a property, that property becomes a keyword that you can use in the route.

You can't pass a regex in the route, but you can in this anon property.

江南月 2024-08-06 00:34:42

.axd 文件是由 HTTP 处理程序处理的虚拟文件。 它们用于(除其他外)向网页提供各种资源,例如为 AJAX 控件自动生成的 javascript 等。

由于这些是虚拟文件,您不希望路由引擎尝试将这些请求映射到控制器。 您需要它们直接由 ASP.NET 执行。

这就是该生产线所实现的目标。

An .axd file is a virtual file that is handled by an HTTP Handler. They are used for (amongst other things) delivering various resources to the webpage, such as automatically generated javascript for AJAX controls and the like.

As these are virtual files, you do not want the routing engine to try to map these requests to controllers. You need them to be executed directly by ASP.NET.

That is what the line achieves.

把昨日还给我 2024-08-06 00:34:42

如果没有这个,ASP.NET 将尝试将所有对 AXD 处理程序的请求映射到控制器和操作。 拥有ignoreRoute意味着URL不会按照默认行为将URL映射到控制器。

Without this ASP.NET would try to map all requests to AXD handlers to controllers and actions. Having the ignoreRoute means the URL will not map the URL to a controller as per the default behaviour.

以歌曲疗慰 2024-08-06 00:34:42

它告诉路由引擎忽略此请求并将其留给 ASP.NET Webforms 来处理。

这对于使用处理程序 elmah.axd 的 ELMAH 日志记录非常有用。

It tells the routing engine to ignore this request and leave it to ASP.NET Webforms to handle things.

This is useful for example ELMAH logging that uses the handler elmah.axd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文