0

我最初在我的 RouteConfig 中有这个 maproute

            routes.MapRoute(
        name: "thread",              
        url: "{Areamix}/{urltitle}/{id}/thread",
        defaults: new {controller = "thread", action = "view"
        });

这条路线太长了,所以我把它缩短到

            routes.MapRoute(
        name: "thread",              
        url: "{urltitle}/{Areamix}-{id}",
        defaults: new
        {
            controller = "thread",
            action = "view"
        });

正如您已经知道,由于路由 URL 已更改,旧网页现在返回 404 错误,我怎样才能将旧索引页面重定向或永久重定向到较新的 MapRoute ?他们都有共同的特征,比如{id}任何建议都会非常感谢。

4

1 回答 1

0

保留旧的 MapRoute 原样,但将其映射到新的控制器(现在的操作Redirectview

    routes.MapRoute(
      name: "thread",              
      url: "{Areamix}/{urltitle}/{id}/thread",
      defaults: new {controller = "thread", action = "Redirect" // Action is now Redirect (instead of view)
    });

然后,在 Controller 中创建一个新的 Action:

public ActionResult Redirect()
{
    // Now rebuild your url in new format:
    var link = RouteData.Values["urltitle"] + RouteData.Values["Areamix"] + "-" + RouteData.Values["id"];
    return RedirectPermanent(link);
}

现在所有匹配旧 maproute 的请求都进入重定向操作,然后被格式化为新的 maproute 格式。

我没有测试它,但它应该像那样工作。

于 2014-12-30T22:33:04.300 回答