I want to make a route where the controller name is not displayed in the URL.
I started with this route:
routes.MapRoute(
name: "ViewTag",
url: "tag/tagged/{tag}",
defaults: new { controller = "Tag", action = "Tagged" }
);
And controller method:
public class TagController : Controller
{
public ActionResult Tagged(string tag)
{
Which works but generates URL: tag/tagged/money
I wanted to use the URL: tagged/money
I changed the route to remove the controller name:
routes.MapRoute(
name: "ViewTag",
url: "tagged/{tag}",
defaults: new { controller = "Tag", action = "Tagged" }
);
Which does route correctly when I manually type in the URL: tagged/money
but, it doesn't generate the correct routes.
@Html.ActionLink(tag.Text, "tagged", new { @tag = tag.Text })
or
@Html.ActionLink(tag.Text, "tagged", "tag", new { @tag = tag.Text }, null)
Both generate the original and wrong URL: tag/tagged/{tag}
I thought the route config was used to both parse incoming URL's and generate URL's?
从路由URL删除tagged
后,可以使用Html.RouteLink方法:
@Html.RouteLink(tag.Text, "ViewTag", new { @tag = tag.Text })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.