ASP.NET MVC routing and static data (i.e. Images, scripts, etc.)

If I have a resource request in my ASP.NET MVC1 (or 2) web application for a static resource, say ... image or javascript file or css file ... does the .NET framework try and see if the request matches the list routes ... and as a result cannot find the controller for it?

eg.

Resource: /Content/Images/Foo.png

Does this request go through the list of routes .. can not match any controllers / actions with this request, and then try this path directly?

+6
asp.net-mvc static-resource routing
source share
3 answers

You can choose whether to display an existing file or not set the RouteCollection.RouteExistingFiles Property

Gets or sets a value indicating whether ASP.NET routing URLs matching an existing file should be routed.

Here is what I read from here :

However, the routing system still checks the file system to make sure the incoming URL matches the file or disk, and if so, the routing ignores the request (bypassing any route entries that might match the URL) so the file will be serviced directly. This is very useful for static files such as images, CSS, and JavaScript files. You can save them in your project (for example, in the / Content or / Script folders), and then specify and serve them directly, just as if you had not used routing at all. Because the file does exist on the disk, which takes precedence over the routing configuration.

If instead you want your routing configuration to take precedence over files on disk, you can set the RouteCollections RouteExistingFiles property to true. (By default, it is false.)

+7
source share

By default, the routing mechanism will ignore route maps for all files that physically exist on the server. In short, you don't need to do anything to make the MVC application reference static files.

+1
source share

You can also do a little trick in IIS. I store js, images, css etc. In the Content folder under the virtual directory.

If you then view the properties (in IIS Manager) of the Content folder, create it as a virtual directory, and then remove the wildcard mapping. Then set the Content folder back to the regular directory. Then this should stop requesting for these files handled by the aspnet_isapi handler.

0
source share

All Articles