asp.net-mvc 什么是 routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9016650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
提问by Eric Yin
What is routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
什么是 routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
I cannot find any .axd file in my project, can I remove this route rule?
我在我的项目中找不到任何 .axd 文件,我可以删除此路由规则吗?
采纳答案by Roy Dictus
.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.
.axd 文件在物理上不存在。ASP.NET 在内部使用带有 .axd 扩展名(ScriptResource.axd 和 WebResource.axd)的 URL,它们由 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 MVC 尝试处理请求,而不是让专用的 HttpHandler 来处理。
回答by CodingYoshi
Some Background
一些背景
If you open up this file:
如果你打开这个文件:
%WINDIR%\Microsoft.NET\Framework\version\Config\Web.config
you will find this within the file:
你会在文件中找到这个:
<add path="WebResource.axd"
verb="GET"
type="System.Web.Handlers.AssemblyResourceLoader"
validate="True" />
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."
这基本上是在告诉 Asp.NET 运行时:“嘿 asp.net 伙计,如果请求来自 WebResource.axd,则使用 AssemblyResourceLoader 来处理请求。”
Please do note that WebResource.axdis NOT a filebut 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:
请注意,WebResource.axd不是一个文件,而只是一个到AssemblyResourceLoader. 它是注册处理程序的名称。在我的机器上,我找到了以下 .axd 处理程序:
<add path="eurl.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="True" />
<add path="trace.axd" verb="*" type="System.Web.Handlers.TraceHandler" validate="True" />
<add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="True" />
<add verb="*" path="*_AppService.axd"
Ok, so what does that handler do?
好的,那个处理程序是做什么的?
The AssemblyResourceLoaderknows 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:
该AssemblyResourceLoader知道如何寻找嵌入文件内的组件,因此它可以成为它(将其发送到客户端,即浏览器)。例如,在asp.net web 表单中,如果您使用验证控件,它们依赖于一些javascript 来显示网页上的错误。但是,该 javascript 嵌入在程序集中。浏览器需要 javascript,因此您将在页面的 html 中看到:
<script src="/YourSite/WebResource.axd?d=fs7zUa...&t=6342..." type="text/javascript"></script>
The AssemblyResourceLoaderwill find the assembly where the javascript is embedded using the information in the querystring and return the javascript.
该AssemblyResourceLoader会发现其中的JavaScript使用的查询字符串信息嵌入装配和返回的JavaScript。
Back to the Question
回到问题
So to answer the question, what is:
所以要回答这个问题,什么是:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
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 .axdrequests. Why? Because MVC itself is an HttpHandler similar to .axdand .aspxand 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-the AssemblyResourceLoaderknows how to do that. MVC knows how to do, well everything it does which is beyond the scope of this question and answer.
这告诉路由引擎我们不会处理那些匹配该路由模式的请求。换句话说,我们不会处理.axd请求。为什么?因为MVC本身类似于一个HttpHandler.axd和.aspx那些在web.config文件和许多其他处理。MVC 处理程序不知道如何处理请求,例如在程序集中寻找嵌入的资源——他们AssemblyResourceLoader知道如何做到这一点。MVC 知道如何做,它所做的一切都超出了本问答的范围。
回答by Kumar Manish
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.
包含模式 {resource}.axd/{*pathInfo} 的路由是为了防止对 Web 资源文件(例如 WebResource.axd 或 ScriptResource.axd)的请求传递给控制器。
Read link: http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
阅读链接:http: //msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
You can also specify that routing should not handle certain URL requests. You prevent routing from handling certain requests by defining a route and specifying that the StopRoutingHandlerclass should be used to handle that pattern. When a request is handled by a StopRoutingHandlerobject, the StopRoutingHandlerobject blocks any additional processing of the request as a route. Instead, the request is processed as an ASP.NET page, Web service, or other ASP.NET endpoint. You can use the RouteCollection.Ignoremethod (or RouteCollectionExtensions.IgnoreRoutefor MVC applications) to create routes that use the StopRoutingHandlerclass.
您还可以指定路由不应处理某些 URL 请求。您可以通过定义路由并指定应使用StopRoutingHandler类来处理该模式来阻止路由处理某些请求。当请求由StopRoutingHandler对象处理时,StopRoutingHandler对象会阻止对请求的任何附加处理作为路由。而是将请求作为 ASP.NET 页、Web 服务或其他 ASP.NET 端点进行处理。您可以使用RouteCollection.Ignore方法(或RouteCollectionExtensions.IgnoreRoute对于 MVC 应用程序)来创建使用StopRoutingHandler类的路由。
回答by Kumar Manish
Those are not files (they don't exist on disk) - they are just names under which some HTTP handlers are registered.
这些不是文件(它们不存在于磁盘上)——它们只是一些 HTTP 处理程序注册的名称。

