创建类似于 Stack Overflow 的“此问题的短永久链接”的短永久链接

发布于 2024-09-29 20:15:11 字数 621 浏览 9 评论 0原文

我想我可能已经明白这是如何工作的,但想确定一下。

我正在为新的 ASP.NET MVC 应用程序定义路由。我想创建类似于 Stack Overflow 的此问题的短永久链接的短永久链接:

创建类似于 Stack Overflow 的“此问题的短永久链接”的短永久链接

Stack Overflow 为此永久链接使用什么路由和控制器机制行为?

讨论 Stack Overflow 问题路线的其他问题:

I think I might already understand how this works, but wanted to be sure.

I am in the process of defining the routes for a new ASP.NET MVC application. I'd like to create short permalinks similar to Stack Overflow's short permalink to this question:

Create short permalinks similar to Stack Overflow's "short permalink to this question"

What route and controller mechanism is Stack Overflow using for this permalink behavior?

Other Questions discussing Stack Overflow question routes:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

愿与i 2024-10-06 20:15:11

我相信 Stack Overflow 路由的设置与此类似:

routes.MapRoute("question-permalink", "q/{questionId}/{userId}", 
    new { controller = "PermaLinkController",
        action = "Question", userId = UrlParameter.Optional },
    new { questionId = "[0-9]+", userId = "[0-9]+" });

基于指向问题当前位置的 302 Found:我假设 PermaLink 控制器的 Question 操作如下所示:

public class PermaLinkController : Controller
{
    public Question (int questionId, int? userId)
    {
        // do work to record userId that shared link
        // ...
        // now redirect
        Response.RedirectToRoute("question", new { questionId = questionId });
    }
}

I believe that the Stack Overflow routes are setup something similar to this:

routes.MapRoute("question-permalink", "q/{questionId}/{userId}", 
    new { controller = "PermaLinkController",
        action = "Question", userId = UrlParameter.Optional },
    new { questionId = "[0-9]+", userId = "[0-9]+" });

Based on the 302 Found pointing to the question's current location: I assume the PermaLink controller's Question action looks something like this:

public class PermaLinkController : Controller
{
    public Question (int questionId, int? userId)
    {
        // do work to record userId that shared link
        // ...
        // now redirect
        Response.RedirectToRoute("question", new { questionId = questionId });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文