什么是routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”)
什么是 routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
我在项目中找不到任何 .axd 文件,我可以删除此路由规则吗?
What is routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
I cannot find any .axd file in my project, can I remove this route rule?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
.axd 文件实际上并不存在。 ASP.NET 在内部使用带有 .axd 扩展名的 URL(ScriptResource.axd 和 WebResource.axd),并且它们由 HttpHandler 处理。
因此,您应该保留此规则,以防止 ASP.NET MVC 尝试处理请求,而不是让专用的 HttpHandler 来处理。
.axd files don't exist physically. ASP.NET uses URLs with .axd extensions (ScriptResource.axd and WebResource.axd) internally, and they are handled by an HttpHandler.
Therefore, you should keep this rule, to prevent ASP.NET MVC from trying to handle the request instead of letting the dedicated HttpHandler do it.
一些背景
如果您打开此文件:
您将在文件中找到以下内容:
这基本上是在告诉 Asp.NET 运行时:“嘿 asp.net 兄弟,如果请求来自 WebResource.axd,那么使用 AssemblyResourceLoader 来处理请求。”
请注意,WebResource.axd 不是一个文件,而只是一个到
AssemblyResourceLoader
的映射(如果我可以这么说的话)。这是处理程序注册时使用的名称。在我的机器上,我发现了以下 .axd 处理程序:好吧,那么该处理程序是做什么的?
AssemblyResourceLoader
知道如何在程序集中查找嵌入文件,以便它可以提供服务(将其发送到客户端,即浏览器)。例如,在 ASP.NET Web 表单中,如果您使用验证控件,它们将依赖于某些 JavaScript 来在网页上显示错误。但是,该 javascript 嵌入在程序集中。浏览器需要 javascript,因此您将在页面的 html 中看到这一点:AssemblyResourceLoader
将使用查询字符串中的信息找到嵌入 javascript 的程序集并返回 javascript。回到问题
所以要回答这个问题,什么是:
这告诉路由引擎我们不会处理那些与该路由模式匹配的请求。换句话说,我们不会处理
.axd
请求。为什么?因为 MVC 本身是一个 HttpHandler,类似于.axd
和.aspx
以及 web.config 文件中的许多其他处理程序。 MVC 处理程序不知道如何处理请求,例如在程序集中查找嵌入资源 -AssemblyResourceLoader
知道如何执行此操作。 MVC 知道如何做,以及它所做的一切,这超出了本问答的范围。Some Background
If you open up this file:
you will find this within the file:
That is basically telling the Asp.NET runtime: "Hey asp.net dude, if a request comes for WebResource.axd then use AssemblyResourceLoader to process the request."
Please do note that WebResource.axd is NOT a file but simply a map (if I may say) to
AssemblyResourceLoader
. It is the name under which the handler is registered. On my machine, I found the following .axd handlers:Ok, so what does that handler do?
The
AssemblyResourceLoader
knows how to look for embedded files within an assembly so it can serve it (send it to the client i.e. a browser). For example, in asp.net web forms, if you use the validation controls, they depend on some javascript to show the errors on the web page. However, that javascript is embedded in an assembly. The browser needs the javascript so you will see this in the html of the page:The
AssemblyResourceLoader
will find the assembly where the javascript is embedded using the information in the querystring and return the javascript.Back to the Question
So to answer the question, what is:
That is telling the routing engine that we will not be processing those requests that match that route pattern. In other words, we will not process
.axd
requests. Why? Because MVC itself is an HttpHandler similar to.axd
and.aspx
and many other handlers that are in the web.config file. The MVC handler does not know how to process the request such as looking for embedded resources in an assembly-theAssemblyResourceLoader
knows how to do that. MVC knows how to do, well everything it does which is beyond the scope of this question and answer.包含模式为 {resource}.axd/{*pathInfo} 的路由是为了防止将 WebResource.axd 或 ScriptResource.axd 等 Web 资源文件的请求传递到控制器。
阅读链接:
http://msdn.microsoft.com/en-us/library/ cc668201%28v=vs.100%29.aspx
The route with the pattern {resource}.axd/{*pathInfo} is included to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.
Read link:
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
看看下面的链接:
http://haacked.com/archive/ 2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx
Take a look in the below link:
http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx
这些不是文件(它们不存在于磁盘上)——它们只是注册一些 HTTP 处理程序的名称。
Those are not files (they don't exist on disk) - they are just names under which some HTTP handlers are registered.