【问题标题】:How redirect to MVC routes using a old asp site url correctly?如何正确使用旧的 asp 站点 url 重定向到 MVC 路由?
【发布时间】:2019-04-18 02:43:44
【问题描述】:

让我解释一下。最近,我将一个经典的 asp 应用程序迁移到 .net MVC4。旧网站由很多html和aspx页面组成,即:

  • index.html
  • book.html
  • contactus.aspx

当然,旧的 ASP 网站是通过 www.library.com/index.html、www.library.com/book.html、www.library.com/contactus.aspx 等网址访问的等。

现在,通过路由器访问当前 MVC 站点,即 www.library.com/Home、www.library.com/Book、www.library.com/Contact

很多公司客户尝试使用旧网址访问新网站,这是一个问题,我需要从代码中找到解决方案。

我尝试了一些解决方案,但不起作用。

解决方案一

对于旧的 aspxs 页面,可以在新站点根目录上创建与旧站点同名的 aspx 页面并从代码隐藏在 Page_Load 上重定向

//Create contactus.aspx on root directory project and redirect
public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.RedirectToRoute(new { controller = "Contact", action = "Index" });
    }
} 

对于 html 页面,您可以在元标记上定义一个简单的刷新

//Create a html page, i.e., index.html
<meta http-equiv="refresh" content="0; url=http://library.com/Home" />
<p><a href="http://example.com/">Redirect</a></p>

这不是一个可行的解决方案,它需要为每个需要的重定向创建一个 html 页面或 aspx 页面。

解决方案 2

我尝试在 Global.asax 上定义重定向条件,但不起作用

    protected void Application_BeginRequest(object sender, EventArgs e )
    {
        string url = Request.Url.ToString().ToLower();

        if (url.Contains("index.html"))
            Context.Response.RedirectPermanent("/Home");

        else if (url.Contains("book.html"))
            Context.Response.RedirectPermanent("/Book");

        else if (url.Contains("contactus.aspx"))
            Context.Response.RedirectPermanent("/Contact");
    }

解决方案 3

我尝试在 RouteConfig.cs 上为重定向定义映射,但也不起作用

        //Trying redirect on RegisterRoutes() in RouteConfig.cs
        routes.MapRoute(
            name: "Home",
            url: "index.html",
            defaults: new { controller = "Home", action = "Index" }
        );

        routes.MapRoute(
            name: "Book",
            url: "book.html",
            defaults: new { controller = "Book", action = "Index" }
        );

        routes.MapRoute(
            name: "Contact",
            url: "contactus.aspx",
            defaults: new { controller = "Contact", action = "Index" }
        );

当用户尝试正确访问旧的 asp 站点 url 时如何重定向到新的 mvc 路由?需要快速且经济的解决方案,感谢您的帮助。

【问题讨论】:

  • 您尝试解决方案 3 时发生了什么?
  • 根本不起作用,不要重定向,显示未找到的页面

标签: c# asp.net .net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您需要基于正则表达式模式的 Url Rewrite。请参阅此thread,OP (@Eilimint) 已更新内联解决方案。这可能是一个优雅的解决方案,允许我们在不更改代码的情况下进行覆盖。

<rule name="Redirect to new domain" stopProcessing="true">
  <match url="^(.*)$" />
  <conditions>            
    <add input="{HTTP_HOST}" matchType="Pattern" pattern="^olddomain(:\d+)?$" />
  </conditions>
  <action type="Redirect" url="https://my.newdomain.io/{R:1}" redirectType="Permanent" />
</rule>

【讨论】:

    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多