创建类似于Stack Overflow的短固定链接"此问题的短暂固定链接"

ahs*_*ele 5 asp.net-mvc response.redirect permalinks url-routing short-url

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

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

创建类似于Stack Overflow的短固定链接"此问题的短暂固定链接"

Stack Overflow使用什么路由和控制器机制来实现这种固定链接行为?

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

ahs*_*ele 1

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

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

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

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 });
    }
}
Run Code Online (Sandbox Code Playgroud)