Html.ActionLink showing query url instead of pretty url
The Html.ActionLink
<li> ${Html.ActionLink<HomeC开发者_开发技巧ontroller>(c => c.Edit(ViewData.Model.Id, ViewData.Model.Title), "Edit")} </li>
When created as html shows the URL to be Edit/5006?title=One
. How do I change this to a pretty URL like Edit/5006/One
?
My Edit Action method is
public ActionResult Edit(int id, string title)
You need to have a route setup:
routes.MapRoute(
"DefaultWithTitle",
"{controller}/{action}/{id}/{title}",
new
{
controller = "Home",
action = "Edit",
id = UrlParameter.Optional,
title = UrlParameter.Optional
}
);
It is not depends on the function stamp, but it depends on the routing configuration.
routes.MapRoute("Edit", // Route name
"{controller}/{action}/{id}/{title}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Take a look at the first answer to this question: HTML.ActionLink method
The important point is that you have to make sure you're using the right overload for ActionLink().
精彩评论