Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC routing and static data (ie. images, scripts, etc)

If I have a request for a resource in my ASP.NET MVC1 (or 2) web app for a static resource, say ... an image or a javascript file or a css file ... does the .NET framework try and see if the request matches the route list ... and eventually can't find a controller for it?

eg.

Resource: /Content/Images/Foo.png

Does this request go through my route list .. fails to match any controllers / actions to this request and then attempt that path directly?

like image 921
Pure.Krome Avatar asked Apr 21 '10 08:04

Pure.Krome


People also ask

What is routing in ASP.NET MVC with examples?

Basically, Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table.

What is routing and its types in MVC?

Routing is how ASP.NET MVC matches a URI to an action. MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application.

What is route in ASP.NET MVC?

The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.

How many routes are there in MVC?

Every ASP.NET MVC application must configure (register) at least one route in the RouteConfig class and by default, the ASP.NET MVC Framework provides one default route. But you can configure as many routes as you want.


2 Answers

You can choose whether to map an existing file or not setting the RouteCollection.RouteExistingFiles Property

Gets or sets a value that indicates whether ASP.NET routing should handle URLs that match an existing file.

Here is what I read from here:

However, the routing system still does check the file system to see if an incoming URL happens to match a file or disk, and if so, routing ignores the request (bypassing any route entries that the URL might also match) so that the file will be served directly. This is very convenient for static files, such as images, CSS, and JavaScript files. You can keep them in your project (e.g., in your /Content or /Script folders), and then reference and serve them directly, just as if you were not using routing at all. Since the file genuinely exists on disk, that takes priority over your routing configuration.

If, instead, you want your routing configuration to take priority over files on disk, you can set the RouteCollection’s RouteExistingFiles property to true. (It’s false by default.)

like image 138
Svetlozar Angelov Avatar answered Nov 23 '22 23:11

Svetlozar Angelov


By default the routing engine will ignore route maps for all files that exist physically on the server. In short, you need to do nothing for a MVC app to link to static files.

like image 39
Jay Shanker Avatar answered Nov 23 '22 23:11

Jay Shanker