向 MVC 路由添加冗余信息
当您遇到这个问题时,您会注意到问题的标题位于地址栏中,以及您单击到达此处的链接。 我不确定确切的术语,因此很难搜索,但我该如何做类似的事情? 也就是说,如何将数据添加到纯粹用于显示/搜索引擎的地址栏。
谢谢
As you come to this question you'll notice the title of the question is in the address bar and the link you clicked on to get here. I am not sure the exact terminology so found it difficult to search for but how can I do something similar? That is, How can I add data to the address bar which is purely for show/search engines.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以像这样的 Stack Overflow 问题为例,URL 为:
so.com /questions/1142480/adding-redundant-information-to-a-mvc-route
但是,URL 的功能部分是:
so.com/questions/1142480
实现这一点的方法是定义一个这样的路由:
然后创建一个指向它的链接,如下所示:
我想 URL 标题是通过小写从实际标题创建的,用破折号和其他一些东西替换空格(转义/删除坏字符)。
只要您的 SEO 路线出现在任何其他匹配路线之前,就会使用 SEO 路线。
为了完全清楚起见,控制器实际上是这样的:
Taking the example of a Stack Overflow question like this one the URL is:
so.com/questions/1142480/adding-redundant-information-to-a-mvc-route
However, the functional part of the URL is:
so.com/questions/1142480
The way this is achieved is by defining a route like this:
You then create a link to it like this:
I would imagine the URL title is created from the actual title by lower casing, replacing spaces with dashes and a couple of other things (escaping/striping bad characters).
So long as your SEO route appears before any other matching route the SEO route will be used.
For complete clarity the controller would actually be like this:
您应该意识到的一件事是,此 URL 末尾的文本实际上是一个虚拟文本。 例如,以下 URL:
将清楚地打开此问题。 同样,除了您的问题之外的标题:
也会打开此内容问题没有错误。
您可以轻松地使用一些标题解析算法来生成一个带有标题的“SEO 友好”URL,并将其添加到问题编号的末尾。 您的 MVC 路线将忽略最后一部分。
One thing you should realize is that the text at the end of this URL is actually a dummy. For example, this URL:
will open this question cleanly. Similarly, a title other than your question:
will ALSO open this question without errors.
You can easily use some title-parsing algorithm to generate an "SEO friendly" URL for you complete with the title, and add it at the end of the question number. Your MVC route will just ignore the last part.