MVC路由捕获文件名作为参数

时间:2022-02-08 15:16:50

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.

我正在尝试使用MVC生成一个简单的WebDAV服务器,我终于到了需要向用户提供所请求文件的阶段。

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.

我有一个路由设置,处理遍历目录结构“webdav / {* path}”,它工作正常,直到该路径以文件名结束。此时,似乎IIS确定它是静态文件,并尝试从磁盘提供该文件。由于它不在URL中指定的位置,因此返回404错误。

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:

我没有任何更改URL的*,我基本上需要它在表单中,否则Windows资源管理器无法使用它作为映射驱动器:

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;

我已经设置了贪婪匹配的路由,因为目录结构可以有多个级别。我也设置了routes.RouteExistingFiles = true;

This is using IIS Express 8.0 on my development machine.

这是在我的开发机器上使用IIS Express 8.0。

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

我已经设置了一个空白的MVC项目来测试它,这就是RegisterRoutes方法:

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.

所以,转到/ webdav / Test / Test2命中我的控制器中的断点,但是去/webdav/Test/Test2.txt给我一个404。

Any suggestions?

有什么建议么?

2 个解决方案

#1


2  

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

另一种选择是将其添加到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.

我可以保证这适用于IIS 7.5。

For the record, I found this solution here.

为了记录,我在这里找到了这个解决方案。

#2


11  

I needed to add

我需要补充一下

<modules runAllManagedModulesForAllRequests="true">

to the web config.

到网络配置。

Ah, I've been struggling with this for a few days now, I knew posting here would shift the blockage!

啊,我已经在这几天苦苦挣扎了,我知道在这里张贴会改变堵塞!

#1


2  

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

另一种选择是将其添加到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.

我可以保证这适用于IIS 7.5。

For the record, I found this solution here.

为了记录,我在这里找到了这个解决方案。

#2


11  

I needed to add

我需要补充一下

<modules runAllManagedModulesForAllRequests="true">

to the web config.

到网络配置。

Ah, I've been struggling with this for a few days now, I knew posting here would shift the blockage!

啊,我已经在这几天苦苦挣扎了,我知道在这里张贴会改变堵塞!