开发者

ASP.NET MVC: How to "understand" legacy URLs but not write them?

开发者 https://www.devze.com 2022-12-14 21:21 出处:网络
Please excuse the confusing title ;) We\'ve built an ASP.NET MVC application to replace our \"old\" ASP.NET webforms monster. We still want to support some of the old URLs used there, because these m

Please excuse the confusing title ;)

We've built an ASP.NET MVC application to replace our "old" ASP.NET webforms monster. We still want to support some of the old URLs used there, because these may still float around in E-Mails etc. somewhere out there.

For example: Our old system used the URL ~/Password.aspx, the URL in the new MVC system is ~/Home/Password.

To support these old URLs, we just added them to the route table, like

    routes.MapRoute(
        "Password",
        "Password.aspx",
        new {
            controller = "Home",
            action = "Password",
            id = ""
        }
    );

This works great, but whenever we use Url.Action("Pa开发者_JAVA技巧ssword", "Home") in our code, it generates the old URL ("~/Password.aspx" instead of "~/Home/Password"). Since we want to slowly phase out the old URLs, it would be nice if the MVC routes still "understood" the old URLs but only use/write the new ones.

Is there a way to achieve this?


Put the new route above the old route on the routing table. The higher up the route is, the more precedence it will have:

routes.MapRoute(
      "PasswordNew",
      "Home/Password",
      new {controller = "Home",
           action = "Password",
           id = "" 
       }
    );

routes.MapRoute(
        "Password",
        "Password.aspx",
        new {
            controller = "Home",
            action = "Password",
            id = ""
        }
    );
0

精彩评论

暂无评论...
验证码 换一张
取 消