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

馋奶兔 提交于 2019-12-30 03:12:10

问题


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.


回答1:


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.




回答2:


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




回答3:


The RouteExistingFiles doesn't keep files from being viewed if there is no route for them, it just checks the routes before checking if the file exists. If there is no matching route, it will continue to check if there is a matching file.



来源:https://stackoverflow.com/questions/13905864/asp-net-mvc-3-routeexistingfiles-true-seems-to-have-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!