Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC routing when a file actually exists at the specified location

So I have a route like this in my MVC 3 application running under IIS 7:

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

The trick is that a file actually exists at /virtualtour/config.xml. It seems like the request is just returning the xml file at that location instead of hitting the route, which processes the XML, makes some changes and returns a custom XmlResult.

Any suggestions on how I can tell my application to hit the route and not the actual file in the event that the file exists on disk?

EDIT: It appears that I can use routes.RouteExistingFiles = true; in the RegisterRoutes method of Global.asax to tell the application to ignore files on disk. This, however, sets the flag globally and breaks a lot of other requests within the application. For example, I still want calls to /assets/css/site.css to return the CSS file without having to specifically set routes up for each static asset. So now the question becomes, is there a way to do this on a per-route basis?

like image 805
Scott Avatar asked Jan 13 '11 15:01

Scott


1 Answers

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.

like image 117
Scott Avatar answered Nov 04 '22 14:11

Scott