Route static file to MVC route when the file exists on disk

Vote:
 

I'm unable to route a static file to an MVC route when the file exists on disk, although it works fine if the file isn't there.

This is what I've done:

Overriding the RegisterRoutes in Global.asax

protected override void RegisterRoutes(RouteCollection routes)
	    {
		    base.RegisterRoutes(routes);
		    new RouteConfig().RegisterRoutes(routes);
	    }

Added RouteConfig.cs

public void RegisterRoutes(RouteCollection routes)
		{
			routes.RouteExistingFiles = true;

			routes.MapRoute(
				"foo",
				"foo.pdf",
				new { controller = "Foo", action = "Index" }
				);
		}

Added a handler in system.webServer/handlers

According to Jon Galloway is should work if you add the routes.RouteExistingFiles = true, but I still get an error. Any ideas on how to fix it?

#154871
Aug 31, 2016 18:40
Vote:
 

Have you tried adding the route first in the route table? By switching line 3 and 4 in your first snippet.

#154872
Aug 31, 2016 20:18
Vote:
 

Yes, still get the same error.

#154887
Sep 01, 2016 9:15
Vote:
 

There is another route that captures the request before your one (not a content route added by base.RegisterRoutes). You can however get your route before the other one by doing:

            var route = routes.MapRoute(
                "foo",
                "foo.pdf",
                new { controller = "Foo", action = "Index" }
                );
            routes.Remove(route);
            routes.Insert(0, route);



#157806
Sep 22, 2016 14:20
Vote:
 

Thanks Johan, I got an answer back from Epi support who said there's a bug but suggested that workaround. It works, although it messed up a few other routes so had to fix those as well.

#160559
Sep 28, 2016 16:03
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.