【发布时间】:2014-04-04 07:11:12
【问题描述】:
我想在我的网站上实现StackOverflow 类型的 URL 重定向。
例如
当您在浏览器中输入https://stackoverflow.com/questions/11227809/ 时,它会自动将您重定向到https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array。
即使您输入废品值代替标题,例如https://stackoverflow.com/questions/11227809/askl;fl;aksf,它也会根据帖子编号11227809 将您重定向到正确的值。
这是哪种类型的重定向?那是301 我知道,但是如何使用 .Net MVC 来实现这一点?
我在这里看到了一个非 MVC 解决方案Here 但需要一个基于 MVC 的解决方案。 谢谢。
My Route:
routes.MapRoute("QuestionUrlDetermine", "{qId}/{questionTitle}", new { controller = "Home", action = "QuestionUrlDetermine", questionTitle = UrlParameter.Optional }, new { qId = @"^\d{1,3}$" });
Action:
public ActionResult QuestionUrlDetermine (int qId)
{
…
return new RedirectResult(string.Format("/{0}/{1}", qId, questionTitle));
}
【问题讨论】:
-
我找不到原始答案,但这是相似的:stackoverflow.com/questions/18784979/…
-
我已经用代码更新了我的问题,请检查。
标签: .net asp.net-mvc asp.net-mvc-4 redirect