5

我想我可能已经了解它是如何工作的,但想确定一下。

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

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

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

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

4

1 回答 1

1

我相信堆栈溢出路由的设置类似于以下内容:

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 控制器的问题操作看起来像这样:

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 });
    }
}
于 2010-11-01T22:13:29.207 回答