了解 Global.asax 中的路由 (asp.net-mvc)
在 Global.asax 中以下含义是什么?
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
In Global.asax what does the following signify?
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是学习 MVC 时真正令人沮丧的事情之一 - 此功能的文档非常糟糕 - 几乎没有任何内容: http://msdn.microsoft.com/en-us/library/dd470170(VS.100).aspx。
这允许所有的something.axd文件在MVC之外运行——最后的“{*pathInfo}”允许忽略查询字符串(它是一种通配符)。
请注意,这不会将任何此类通配符应用于路径,因此:
有帮助。 除了“{resource}”和“{*pathInfo}”之外,我无法找到任何像样的文档来说明什么是支持的关键字,什么是不支持的关键字,
但是有一个几乎完全未记录的功能,可以为您提供更多的控制权在这些被忽略的路由上:
如果您传递带有属性的未初始化对象,则该属性将成为您可以在路由中使用的关键字。
您不能在路线中传递正则表达式,但可以在此匿名属性中传递。
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.
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:
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:
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.
.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.
如果没有这个,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.
它告诉路由引擎忽略此请求并将其留给 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.