以 Stack Overflow 问题为例,URL 是:
so.com/questions/1142480/adding-redundant-information-to-a-mvc-route https://stackoverflow.com/questions/1142480/adding-redundant-information-to-a-mvc-route
然而,URL 的功能部分是:
so.com/questions/1142480 https://stackoverflow.com/questions/1142480
实现此目的的方法是定义如下路线:
routes.MapRoute(
"questions",
"questions/{id}/{title}",
new { controller = "Questions", action = "Details", title = "" });
然后您可以像这样创建一个指向它的链接:
<%= Html.RouteLink("Adding Redundant Information to a MVC Route",
new
{
controller = "Questions",
id = 1142480,
title = "adding-redundant-information-to-a-mvc-route"
}
)
%>
我想 URL 标题是根据实际标题通过小写、用破折号和其他一些内容替换空格(转义/删除坏字符)创建的。
只要您的 SEO 路线出现在任何其他匹配路线之前,就会使用 SEO 路线。
为了完全清楚起见,控制器实际上是这样的:
public class QuestionsController : Controller
{
public ActionResult Details(int id)
{
// stuff for display - notice title is not used
}
}