【问题标题】:Create short permalinks similar to Stack Overflow's "short permalink to this question"创建类似于 Stack Overflow 的“此问题的简短永久链接”的简短永久链接
【发布时间】:2011-05-02 03:57:27
【问题描述】:

我想我可能已经了解它的工作原理,但想确定一下。

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

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

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

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

【问题讨论】:

  • 是的,基本上就是这样。此外,这并不是以问答形式提出的——您应该用上面的一些文字来回答您的问题。
  • @Jeff 同意问题格式。我会围绕一个答案重新设计它。感谢您的观看!
  • @Jeff 分成问题和答案。再次感谢您的确认。

标签: asp.net-mvc url-routing permalinks response.redirect short-url


【解决方案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]+" });

基于指向问题当前位置的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 });
    }
}

【讨论】:

  • 这个答案是从上述问题的原始版本中提取的。根据 Jeff 对该问题的评论,它被标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 2013-03-21
  • 1970-01-01
相关资源
最近更新 更多