【发布时间】:2015-07-20 09:59:03
【问题描述】:
我有这样的问题。在 RouteConfig.cs 中,我设置了路由
routes.MapRoute(
"NewsDetails",
"news/details-news/{title}-{id}",
new { controller = "News", action = "Details", id = "", title = "" }
);
在我的 NewsController 的 Index.cshtml 中有一个链接
@Html.RouteLink(item.Title, "NewsDetails", new {
title = MyWeb.Classes.PrettyUrlHelper.PrettyUrl(item.Title),
id = item.Id
})
在我的 NewsController 中:
public ActionResult Details(string title,String id)
{
if (id == null && title == null)
return RedirectToAction("Index");
try
{
int ID = Int32.Parse(id);
var result = NewsConnectionDB.GetInstance().Single<LifeStory>(ID);
return View(result);
}
catch (InvalidOperationException) {
return View("~/Views/Error/Error404.cshtml");
}
catch (FormatException) {
return View("~/Views/Error/Error404.cshtml"); }
}
因此,如果用户单击 View 中的链接,该链接将路由到要处理的操作详细信息,并且该链接是 Seo Url Friendly (localhost:9224/news/details-news/ten-things-2)。但是用户键入链接而不是单击 View 中的链接:
localhost:9224/news/details-news/ten-thingsblahblahblah-2
上面的 url 是正确的 id 但标题不是。那么,如果用户输入了错误的标题但正确的 id,我如何在返回 View 后更新 url?
任何帮助将不胜感激。
P/S:我的英文不好,希望你能理解。
【问题讨论】:
-
在控制器中,用代码重写这个 URL HttpContext.Current.Response.AppendHeader("CorrectUrl", "YourUrl");