URL没有捕获RouteConfig ASP MVC

时间:2015-11-05 16:30:28

标签: asp.net-mvc routes

我有这样的路线

.submenu-ebene2 {
     display: none;
    position:absolute;
  z-index:1000;
}

.submenu-ebene3 {
     display: none; 
    position:absolute;
  z-index:1000;
}

当我调用/ page / white-1时,没关系 我也希望能够捕获URL / page / -1(我有这样的旧URL,我想重定向它们)...但是这个URL / page / -1没有捕获,我有404。

但为什么它没有被捕获而我将参数指定为可选项?我怎么能抓住它?

1 个答案:

答案 0 :(得分:1)

我将定义以连字符开头的第二条路线,然后使用正确的标签重定向到新路线。

name: "XXXXX1",
url: "page/-{id},
defaults: new
    {
        controller = "Page", action = "OldResults",
        id= UrlParameter.Optional
    },

然后在你的控制器中:

public ActionResult OldResults(int id) {
  //goto database get item by id 
  var item = db.getItem(id);
  return RedirectToAction("Results", new {id = id, label = item.label});
}