ASP.NET MVC路由用于具有多个子目录的文件

Cmd*_*len 5 asp.net routing asp.net-mvc-2

我需要设置一个文件处理程序来路由多个子目录,如tihs;

http://localhost/images/7/99/786936215595.jpg
Run Code Online (Sandbox Code Playgroud)

我试着把它放在global.asax文件中;

 routes.Add(
   "ImageRoute",
   new Route("covers/{filepath}/{filename}",
   new ImageRouteHandler()));
Run Code Online (Sandbox Code Playgroud)

我正在使用本问题中的ImageHandler ,如果您有一个子目录(即'/ images/15/786936215595.jpg'),它会很有用,但是当您有多个目录时会失败.

我尝试设置一个通配符,但没有用(即'new Route("cover/{filepath}/*/{filename}"')

这是来自大型NAS的图像(想想像300万张图像)所以它不像我只能移动文件.

谢谢!

Cmd*_*len 3

好吧,经过多次尝试和谷歌之后,我找到了如何让它工作。

像这样更改路由定义;

 routes.Add(
     "ImageRoute",
     new Route("images/{*filepath}",
     new ImageRouteHandler()));
Run Code Online (Sandbox Code Playgroud)

然后将其放在默认 MapRoute 后面。重要的部分是文件路径之前的“*”,它告诉 MVC 将其后的任何内容作为文件路径 RouteData 的一部分发送。因此,在 GetHttpHandler() 方法中,我可以使用以下方法获取完整路径:

string fp = requestContext.RouteData.Values["filepath"] as string;
Run Code Online (Sandbox Code Playgroud)

哇!