Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC route to capture file name as a parameter

Tags:

asp.net-mvc

I am attempting to produce a simple WebDAV server using MVC, and I have finally reached the stage where I need to serve up a requested file to the user.

I have a route set up that deals with traversing the directory structure "webdav/{*path}" which works fine, right up until the point where that path ends in a file name. At this point, it appears that IIS decides that it is a static file, and attempts to serve that file from the disk. As it isn't in the location specified in the URL, it returns a 404 error.

I don't have any freedom to change the url, I basically need it to be in the form, otherwise Windows Explorer can't work with it as a mapped drive:

GET /webdav/Test/Test2.txt

I've set the route to greedily match, as the directory structure can have as many levels. I've also set routes.RouteExistingFiles = true;

This is using IIS Express 8.0 on my development machine.

I've gone as far as setting up a blank MVC project just to test this, and this is the RegisterRoutes method:

routes.RouteExistingFiles = true;

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "WebDAVGet",
    url: "webdav/{*path}",
    defaults: new { controller = "WebDAV", action = "Get", path = "" });

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", 
    id = UrlParameter.Optional }
);

So, going to /webdav/Test/Test2 hits the breakpoint in my controller, but going to /webdav/Test/Test2.txt gives me a 404.

Any suggestions?

like image 290
Paul Manzotti Avatar asked Sep 17 '12 16:09

Paul Manzotti


Video Answer


1 Answers

Another option is to add this to the <system.webserver> node in web.config:

<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>

I can vouch that this works on IIS 7.5.

For the record, I found this solution here.

like image 81
David Kirkland Avatar answered Sep 23 '22 04:09

David Kirkland