什么是routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”)

发布于 2024-12-29 05:30:34 字数 112 浏览 2 评论 0原文

什么是 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 技术交流群。

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

发布评论

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

评论(5

梓梦 2025-01-05 05:30:34

.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.

暗恋未遂 2025-01-05 05:30:34

一些背景

如果您打开此文件:

%WINDIR%\Microsoft.NET\Framework\version\Config\Web.config

您将在文件中找到以下内容:

<add path="WebResource.axd"
     verb="GET"
     type="System.Web.Handlers.AssemblyResourceLoader"
     validate="True" />

这基本上是在告诉 Asp.NET 运行时:“嘿 asp.net 兄弟,如果请求来自 WebResource.axd,那么使用 AssemblyResourceLoader 来处理请求。”

注意,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" 

好吧,那么该处理程序是做什么的?

AssemblyResourceLoader 知道如何在程序集中查找嵌入文件,以便它可以提供服务(将其发送到客户端,即浏览器)。例如,在 ASP.NET Web 表单中,如果您使用验证控件,它们将依赖于某些 JavaScript 来在网页上显示错误。但是,该 javascript 嵌入在程序集中。浏览器需要 javascript,因此您将在页面的 html 中看到这一点:

<script src="/YourSite/WebResource.axd?d=fs7zUa...&t=6342..." type="text/javascript"></script>

AssemblyResourceLoader 将使用查询字符串中的信息找到嵌入 javascript 的程序集并返回 javascript。


回到问题

所以要回答这个问题,什么是:

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

这告诉路由引擎我们不会处理那些与该路由模式匹配的请求。换句话说,我们不会处理 .axd 请求。为什么?因为 MVC 本身是一个 HttpHandler,类似于 .axd.aspx 以及 web.config 文件中的许多其他处理程序。 MVC 处理程序不知道如何处理请求,例如在程序集中查找嵌入资源 - AssemblyResourceLoader 知道如何执行此操作。 MVC 知道如何做,以及它所做的一切,这超出了本问答的范围。

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."

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:

<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 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:

<script src="/YourSite/WebResource.axd?d=fs7zUa...&t=6342..." type="text/javascript"></script>

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:

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 .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-the AssemblyResourceLoader knows how to do that. MVC knows how to do, well everything it does which is beyond the scope of this question and answer.

旧瑾黎汐 2025-01-05 05:30:34

包含模式为 {resource}.axd/{*pathInfo} 的路由是为了防止将 WebResource.axd 或 ScriptResource.axd 等 Web 资源文件的请求传递到控制器。

阅读链接:
http://msdn.microsoft.com/en-us/library/ cc668201%28v=vs.100%29.aspx

您还可以指定路由不应处理某些 URL 请求。您可以通过定义路由并指定 StopRoutingHandler 类应该用于处理该模式。当请求由 StopRoutingHandler 对象,StopRoutingHandler 对象会阻止对请求作为路由。相反,请求将作为 ASP.NET 页面、Web 服务或其他 ASP.NET 端点进行处理。您可以使用 RouteCollection.Ignore< /a> 方法(或 RouteCollectionExtensions.IgnoreRoute 适用于 MVC 应用程序) 创建使用 StopRoutingHandler 类。

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

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 StopRoutingHandler class should be used to handle that pattern. When a request is handled by a StopRoutingHandler object, the StopRoutingHandler object 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.Ignore method (or RouteCollectionExtensions.IgnoreRoute for MVC applications) to create routes that use the StopRoutingHandler class.

陌若浮生 2025-01-05 05:30:34

这些不是文件(它们不存在于磁盘上)——它们只是注册一些 HTTP 处理程序的名称。

Those are not files (they don't exist on disk) - they are just names under which some HTTP handlers are registered.

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