gpt4 book ai didi

asp.net-mvc - 向 MVC 路由添加冗余信息

转载 作者:行者123 更新时间:2023-12-04 17:59:38 25 4
gpt4 key购买 nike

当您遇到这个问题时,您会注意到问题的标题在地址栏中,以及您点击此处的链接。我不确定确切的术语,因此很难搜索,但我该如何做类似的事情?也就是说,如何将数据添加到纯粹用于显示/搜索引擎的地址栏中。

谢谢

最佳答案

以这样一个 Stack Overflow 问题为例,URL 是:

so.com/questions/1142480/adding-redundant-information-to-a-mvc-route

但是,URL 的功能部分是:

so.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 路线。

为了完全清楚, Controller 实际上是这样的:
public class QuestionsController : Controller
{
public ActionResult Details(int id)
{
// stuff for display - notice title is not used
}
}

关于asp.net-mvc - 向 MVC 路由添加冗余信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1142480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com