MVC routing when a file actually exists at the specified location

拜拜、爱过 提交于 2019-11-30 05:13:28

So far the best answer to this that I have found is to globally apply routes.RouteExistingFiles=true and then selectively ignore the routes I want to pass through to existing files like .js, .css, etc. So I ended up with something like this:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("*.js|css|swf");
            routes.RouteExistingFiles = true;

            routes.MapRoute(
               "VirtualTourConfig",
               "virtualtour/config.xml",
               new { controller = "VirtualTour", action = "Config" }
               );
}

If anyone has a better solution, I'd like to see it. I'd much prefer to selectively apply an "RouteExistingFIles" flag to individual routes but I don't know if there's a way to do that.

No solution here, just an idea.

You can try to implement solution based on your own VirtualPathProvider that will provide your own mapping of web paths to file system paths and use default provider for all paths you don't want to take care of.

A very simple solution, certainly if you consider Scott's answer, would be eg: routes.IgnoreRoute("templates/*.html");, routes.IgnoreRoute("scripts/*.js"); and routes.IgnoreRoute("styles/*.css");.

This gives you both the simplicity of just supplying paths to the RoutesCollection and avoids the work of having to initiate eg. a VirtualPathProvider.

You could try UrlRewiting e.g.:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="VirtualTourConfig">
          <match url="^virtualtour/config.xml" />
          <action type="Rewrite" url="virtualtour/config" />
        </rule>
      </rules>
    </rewrite>

NB I'm using this for a different use-case (i.e. serving an angular app from asp.net MVC) - I haven't tested whether MVC routing occurs AFTER url rewriting.

In my case I have a normal MVC route (i.e. /Dashboard/ => DashboardController.Index()) but need all relative paths in the Views/Dashboard/Index.cshtml to serve static files without getting confused by MVC routing i.e. /Dashboard/app/app.module.js must serve a static file. I use UrlRewriting to map /Dashboard/(.+) to /ng/Dashboard/{R:1} as follows:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Dashboard">
          <match url="^dashboard/(.+)" />
          <action type="Rewrite" url="ng/dashboard/{R:1}" />
        </rule>
      </rules>
    </rewrite>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!