向 MVC 路由添加冗余信息

发布于 2024-07-29 04:19:25 字数 124 浏览 3 评论 0原文

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

谢谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-08-05 04:19:25

以像这样的 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 路线。

为了完全清楚起见,控制器实际上是这样的:

public class QuestionsController : Controller
{
    public ActionResult Details(int id)
    {
        // stuff for display - notice title is not used
    }
}

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:

routes.MapRoute(
    "questions",
    "questions/{id}/{title}",
    new { controller = "Questions", action = "Details", title = "" });

You then create a link to it like this:

<%= Html.RouteLink("Adding Redundant Information to a MVC Route", 
        new 
        { 
            controller = "Questions", 
            id = 1142480, 
            title = "adding-redundant-information-to-a-mvc-route" 
        }
    )
%>

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:

public class QuestionsController : Controller
{
    public ActionResult Details(int id)
    {
        // stuff for display - notice title is not used
    }
}
以往的大感动 2024-08-05 04:19:25

您应该意识到的一件事是,此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文