开发者

Add MapRoute for a static resource ASP.NET MVC

开发者 https://www.devze.com 2023-04-11 11:06 出处:网络
I have a pdf file which I would like to a create a route map for it. Is there a way to make object default take a url in stead of action controller combination?

I have a pdf file which I would like to a create a route map for it. Is there a way to make object default take a url in stead of action controller combination?

Instead of

 routes.MapRoute("MyRouteName", "MyNiceUrl", new { controller = "ControllerNam开发者_StackOverflowe", action = "ActionName" });

Have something like

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


You don't need routes for static resources. You need url helpers to reference them:

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

And if you wanted to have an url like /SomeController/MyNiceUrl to serve your pdf file you could simply write a controller action:

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

and then:

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


As in this answer:

Use your controller, or create a mini-controller, and then use the Redirect ActionResult:

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号