MVC路线捕获文件名作为参数 [英] MVC route to capture file name as a parameter

查看:148
本文介绍了MVC路线捕获文件名作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图产生使用MVC一个简单的WebDAV服务器,而我也终于到达,我需要一个请求的文件服务到用户的阶段。

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.

我已经设置了路由与遍历目录结构中的交易的WebDAV / {*路径}工作正常,右,直到点这条道路两端在一个文件名。在这一点上,似乎IIS决定它是一个静态文件,并试图将投放文件从磁盘。因为它不是在URL中指定的位置,则返回404错误。

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.

我没有任何自由更改URL,我基本上需要它的形式,否则Windows资源管理器不能用它作为一个映射的驱动器:

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

我已经设置贪婪地匹配,因为目录结构可以有很多层次的路由。我还设置 routes.RouteExistingFiles = TRUE;

这是使用IIS防爆preSS 8.0我的机器上。

This is using IIS Express 8.0 on my development machine.

我已经尽可能建立一个空MVC项目只是为了测试这个走了,这是的RegisterRoutes方法:

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 }
);

所以,要 / WebDAV的/测试/ Test2的打在我的控制器断点,但将 /webdav/Test/Test2.txt 给我一个404。

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

有什么建议?

推荐答案

另一种选择是把它添加到的<&system.webserver GT;在web.config中的节点:

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>

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

I can vouch that this works on IIS 7.5.

有关记录,我发现这个解决方案这里

For the record, I found this solution here.

这篇关于MVC路线捕获文件名作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆