Route URL Request Using RouteExistingFiles in MVC4

Here we go.

RouteExistingFiles is beneficial for preventing access to the files and allow ASP.Net to handle such requests using Route.

Open your Visual Studio, click on New Project and select ASP.NET MVC4 Web Application.



Specify the name of your first application and click OK.

A new window will appear, from that window I picked up intranet application whilst many options are there. Another intersting fact is that there is a dropdown named View Engine. I seleccted Razor that is more specific to MVC 3 and MVC 4 and kept the checkbox “create a new unit test” unchecked.



Click on the OK button.

The following screen is the default one that is shown after clicking the OK button. We have multiple folders.

Let's go into more detail for this. Routing is defined in Global.asax, open the file and you can see the following lines of code.



We need to make some amendments to this to understand how routing works. The RouteConfig class is responsible for registering the RegisterRoutes method. This Application_Start method is called the ASP.NET platform when the application is started first.

And the default structure is depicted below. I've added two files into the Content folder of the root directory.

themes folder
Let's see what happens when we run our application the first time with the default routing settings.



An idea to write this article is to be able to serve a physical file using a route, not by using a physical file in the site. I've created a route entry in the controller and action methods necessary to serve up the content:

  1. routes.MapRoute(  
  2.       name: "PNGFile"// Route name  
  3.       url: "Content/{file}.png",  
  4.       defaults: new { controller = "Home", action = "Html", file = UrlParameter.Optional }//,// Parameter defaults  
  5.       //namespaces: new[] { "MVCSample.Controllers" }   // Namespaces  
  6.   );  
  7.   
  8.  routes.MapRoute(  
  9.        name: "HtmlFIle"// Route name  
  10.        url: "Content/{file}.html",  
  11.        defaults: new { controller = "Home", action = "Html", file = UrlParameter.Optional }//,// Parameter defaults  
  12.        //namespaces: new[] { "MVCSample.Controllers" }   // Namespaces  
  13.    );  
There are two entries in the RouteConfig.cs file that is pertinent to .png and .html files. So whenever the URL is for a .png or .html file, it will be served using route.

The job of a routing system is to verify the incoming URL request and determine the value of the segment variables. The segment variables are defined using curly braces {}.

All is set so far. Now we'll define a property in the RouteConfig file to execute and verify the functionality.

Kindly refer to the following image to learn more.



Note: In layman terms, whenever a “.png” or “.html” is a part of URLs then this route will take care of it to respond instead of the physical location.

Now I run an applcation and see all the factors. Paste the following URL into a brower and execute.

http://localhost:63005/Content/logo.png

It'll reach to an anticipated action of the home controller as shown in the following image:



The final output would be as shown in the following image:



Now I peform another step and comment the line routes.RouteExistingFiles = true;

From the code to see the effect. Press F5.

Output: It returns an image from the directory instead of using the Route method.

Key Point: It didn't hit an action method of the controller this time.

Note: RouteExistingFiles is beneficial in that scenario when you prevent access to the files and allow ASP.Net to handle such requests using Route.


I hope it'll help you some day.

Enjoy MVC and Routing.

Kindly inform me if you have any query.

Cheers, .Net.

Up Next
    Ebook Download
    View all
    Learn
    View all