【问题标题】:Add MapRoute for a static resource ASP.NET MVC为静态资源 ASP.NET MVC 添加 MapRoute
【发布时间】:2011-10-06 16:47:06
【问题描述】:

我有一个 pdf 文件,我想为它创建一个路线图。有没有办法让对象默认采用 url 而不是动作控制器组合?

代替

 routes.MapRoute("MyRouteName", "MyNiceUrl", new { controller = "ControllerName", action = "ActionName" });

有类似的东西

 routes.MapRoute("MyRouteName", "MyNiceUrl", new { relativeUrl="MyrelativeUrl" });

【问题讨论】:

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


    【解决方案1】:

    静态资源不需要路由。您需要 url 助手来引用它们:

    <a href="<%= Url.Content("~/Content/test.pdf") %>">Download pdf</a>
    

    如果您想使用像 /SomeController/MyNiceUrl 这样的 url 来提供您的 pdf 文件,您可以简单地编写一个控制器操作:

    public ActionResult MyNiceUrl()
    {
        var pdf = Server.MapPath("~/Content/test.pdf");
        return File(pdf, "application/pdf");
    }
    

    然后:

    <%= Html.ActionLink("Download pdf", "MyNiceUrl", "SomeController") %>
    

    【讨论】:

      【解决方案2】:

      this answer

      使用你的控制器,或者创建一个迷你控制器,然后使用 Redirect ActionResult:

      public class MyController : Controller
      {
          public ActionResult Pdf()
          {
              return Redirect( Url.Content( "mydoc.pdf" ) );
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-30
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多