MVC to write the file name as a parameter

I am trying to create a simple WebDAV server using MVC, and I finally reached the stage where I need to transfer the requested file to the user.

I have a route that deals with moving the directory structure "webdav/{*path}" , which works fine, right up until the moment when this path ends with the file name. At the moment, it seems that IIS is deciding that it is a static file, and is trying to serve this file from disk. Since it is not 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 shape, otherwise Windows Explorer cannot work with it as a mapped drive:

 GET /webdav/Test/Test2.txt 

I set the route for greedy matching, since the directory structure can have so many levels. I also set routes.RouteExistingFiles = true;

This uses IIS Express 8.0 on my development machine.

I went as far as creating an empty MVC project 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 gets to a breakpoint in my controller, but going to /webdav/Test/Test2.txt gives me 404.

Any suggestions?

+7
source share
2 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 .

+2
source

I needed to add

 <modules runAllManagedModulesForAllRequests="true"> 

into the web configuration.

And, I struggled with this for several days, I knew that publishing here would move the lock!

+11
source

All Articles