Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3: RouteExistingFiles = true seems to have no effect

I'm trying to understand how RouteExistingFiles works. So I've created a new MVC 3 internet project (MVC 4 behaves the same way) and put a HTMLPage.html file to the Content folder of my project. Now I went to the Global.Asax file and edited the RegisterRoutes function so it looks like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = true; //Look for routes before looking if a static file exists

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );
    }

Now it should give me an error when I'm requesting a localhost:XXXX/Content/HTMLPage.html since there's no "Content" controller and the request definitely hits the default pattern. But instead I'm seeing my HTMLPage. What am I doing wrong here?

Update: I think I'll have to give up. Even if I'm adding a route like this one:

routes.MapRoute("", "Content/{*anything}", new {controller = "Home", action = "Index"});

it still shows me the content of the HTMLPage. When I request a url like ~/Content/HTMLPage I'm getting the Index page as expected, but when I add a file extenstion like .html or .txt the content is shown (or a 404 error if the file does not exist). If anyone can check this in VS2012 please let me know what result you're getting. Thank you.

like image 775
Oleksandr Kaplun Avatar asked Dec 16 '12 21:12

Oleksandr Kaplun


2 Answers

To enabling routing for static files you must perform following steps.

In RouteConfig.cs enable routing for existing files

routes.RouteExistingFiles = true;

Add a route for your path ( Make sure specialized path are above generalized paths)

routes.MapRoute(
            name: "staticFileRoute",
            url: "Public/{file}/",
            defaults: new { controller = "Home", action = "SomeAction" }
        );

Next configure your application, so that request for static files are handeled by "TransferRequestHandler".In Webconfig under system.webServer>handlers add following entry.

<add name="MyCustomUrlHandler2" path="Public/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

The value of 'path' can be more generic or specific depending on your requirement. But i prefer it to be always very specific as per one's need. Keeping it very generic will block serving of other site specific resources such as .js or css files. For example if above is set as path="*", then request for even the css (inside the content folder) which is responsible for how your page would look will also end up in your Controller's action. Something that you will not like.

like image 51
Prerak K Avatar answered Sep 17 '22 15:09

Prerak K


Visual Studio 2012 uses IIS Express. You need to tell IIS not to intercept requests for disk files before they are passed to the MVC routing system. You need set preCondition attribute to the empty string in config file:

<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" 
     preCondition="" />

In Win7/8 you can find config file on this path: %userprofile%\Documents\IISExpress\config\applicationhost.config

like image 37
Igor Avatar answered Sep 18 '22 15:09

Igor