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

67,209

Solution 1

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

Solution 2

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...&amp;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.

Solution 3

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.

Solution 4

Take a look in the below link: http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx

Solution 5

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

Share:
67,209
Eric Yin
Author by

Eric Yin

Updated on February 02, 2020

Comments

  • Eric Yin
    Eric Yin over 4 years

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

    I cannot find any .axd file in my project, can I remove this route rule?

  • Andranik Hovhannisyan
    Andranik Hovhannisyan over 10 years
    I'm pretty sure you'd want people to be able to download the "axd files" on your site as they contain javascript and styles.
  • Sakthivel
    Sakthivel about 9 years
    most relevant answer i would say.
  • NightOwl888
    NightOwl888 almost 8 years
    This answer is incorrect. The reason for putting IgnoreRoute into the routing configuration of MVC is to ensure that MVC doesn't try to handle the request. This is because .axd endpoints need to be handled by another HTTP handler (a handler that is not part of MVC) in order to serve scripts.
  • Yahya
    Yahya over 6 years
    This definitely absolutely certainly should be the accepted answer, plus one from me.
  • Dinerdo
    Dinerdo about 5 years
    Is this entirely unnecessary in .NET core? I have searched on Google for an hour or so and I can't find any references for what the equivalent steps would be, so I'm assuming it's simply not needed.
  • Ben D
    Ben D over 4 years
    Finally, a clear explanation of what WebResource.axd actually does whilst also providing a clear answer to the question. Thank you