ASP.NET MVC:如何“理解”ASP.NET MVC遗留 URL 但不写它们?

发布于 2024-08-16 10:52:57 字数 639 浏览 6 评论 0原文

请原谅这个令人困惑的标题;)

我们构建了一个 ASP.NET MVC 应用程序来取代我们的“旧”ASP.NET Webforms 怪物。我们仍然希望支持那里使用的一些旧 URL,因为这些 URL 可能仍然存在于电子邮件等其他地方。

例如:我们的旧系统使用的URL为~/Password.aspx,新MVC系统中的URL为~/Home/Password。

为了支持这些旧的 URL,我们只是将它们添加到路由表中,就像

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

这样效果很好,但是每当我们在代码中使用 Url.Action("Password", "Home") 时,它都会生成旧的 URL ("~/Password .aspx”而不是“~/Home/Password”)。由于我们希望慢慢淘汰旧 URL,因此如果 MVC 路由仍然“理解”旧 URL 但只使用/写入新 URL,那就太好了。

有办法实现这一点吗?

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("Password", "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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2024-08-23 10:52:57

将新路由放在路由表中旧路由的上方。路由越高,其优先级就越高:

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

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

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 = ""
        }
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文