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 が変更されたため、古い Web ページが 404 エラーを返すようになったことは既にご存じのとおりですが、古いインデックス付きページを新しい MapRoute にリダイレクトまたは永続的にリダイレクトするにはどうすればよいですか? それらはすべて、 {id}などの共通の機能を共有しています。

4

1 に答える 1

0

Leave the old MapRoute as it is but map it to a new Controller (action is now Redirect instead of view:

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

Then, create a new Action in your Controller:

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

Now all requests matching the old maproute go to the Redirect Action and are then formatted to the new maproute format.

I didn't test it but it should work like that.

于 2014-12-30T22:33:04.300 に答える