【问题标题】:ASP.NET MVC Routing for files with muliple sub-directories具有多个子目录的文件的 ASP.NET MVC 路由
【发布时间】:2011-03-31 15:15:53
【问题描述】:

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

http://localhost/images/7/99/786936215595.jpg

我尝试将其放入 global.asax 文件中;

 routes.Add(
   "ImageRoute",
   new Route("covers/{filepath}/{filename}",
   new ImageRouteHandler()));

我正在使用 Question 中的 ImageHandler,如果您有一个子目录(即“/images/15/786936215595.jpg”),它会很好用,但当您有多个目录时会失败。

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

这是从大型 NAS 提供图像(想像 300 万张图像),所以我不能只是移动文件。

谢谢!

【问题讨论】:

    标签: asp.net asp.net-mvc-2 routing


    【解决方案1】:

    好吧,在玩了很多之后,我发现了如何让它工作。

    像这样改变路由定义;

     routes.Add(
         "ImageRoute",
         new Route("images/{*filepath}",
         new ImageRouteHandler()));
    

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

    string fp = requestContext.RouteData.Values["filepath"] as string;
    

    哇!

    【讨论】:

      【解决方案2】:

      您不能将整个路径视为一个路由参数吗?像这样:

      routes.Add(
          "ImageRoute",
          "/images/{path}",
          new { controller = "Image", action = "Image" }
      );
      

      然后在ActionResult Image(string path) { }这个action方法中访问整个路径?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-20
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        • 2019-05-10
        • 2012-08-09
        相关资源
        最近更新 更多