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

孤人 提交于 2019-11-30 08:54:47

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.

Igor

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

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.

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