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" ) );
}
}
精彩评论