【问题标题】:ASP.NET MVC Redirect Old URL to New URLASP.NET MVC 将旧 URL 重定向到新 URL
【发布时间】:2017-09-05 11:20:12
【问题描述】:

我有一个包含一组 html 文件的网站,现在我正在迁移到 MVC 应用程序。我尝试使用博客中提到的代码

http://www.bloggersworld.com/index.php/redirecting-old-urls-to-new-in-aspnet-mvc/

用于将旧 URL 重定向到新 MVC 但出现 404 错误。

这里是 RouteConfig.cs 代码

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.Add(new LegacyUrlRoute());

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

我创建了一个类似博客中提到的类,并设置了提到的重定向。

来自 LegacyUrlRoute 的示例代码

new RedirectRule ("oldpage.html", "", "/department/letter/july")

这里的部门是我的控制器,信是行动,七月是 ID。

如果我直接在 URL 中给出这个 /department/letter/july ,它就可以工作。

我注意到问题在于 URL 中的 .html 而不是 .aspx。

如果我给 URL 作为 localhost/oldpage.aspx 那么它可以正常工作,但如果我给 localhost/oldpage.html 它会转到 404

你能建议吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    必须在 web.config 中添加 HTML 处理程序,这将解决与重定向 HTML URL 相关的问题。

    这里的代码 sn-p 与问题中给出的链接几乎相同。

    RouteConfig.cs

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.Add(new LegacyUrlRoute());
    
            routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
    
        }
    }
    

    LegacyUrlRoute 类:

          public class RedirectRule
            {
                public string OldUrlContains;
                public string OldUrlContainsNot;
                public string NewUrl;
    
                public RedirectRule(string strOldUrlContains, string 
     strOldUrlContainsNot, string strNewUrl)
                {
                    OldUrlContains = strOldUrlContains;
                    OldUrlContainsNot = strOldUrlContainsNot;
                    NewUrl = strNewUrl;
                }
            }
    
    
        public class LegacyUrlRoute : RouteBase
        {
            RedirectRule[] RedirectRules =
            {
                new RedirectRule("oldhtmlurlvalue", "", "/controller/action"),
            };
    
    
            public override RouteData GetRouteData(HttpContextBase httpContext)
            {
                const string status = "301 Moved Permanently";
                var request = httpContext.Request;
                var response = httpContext.Response;
                var legacyUrl = request.Url.ToString();
                var newUrl = "";
    
                foreach (RedirectRule rule in RedirectRules)
                {
                    //if we don't have to check for a string that does not exist in the url
                    if (rule.OldUrlContainsNot.Length == 0)
                    {
    
                        //this does a case insensitive comparison
                        if (legacyUrl.IndexOf(rule.OldUrlContains, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            newUrl = rule.NewUrl;
                        }
                    }
                    else
                    {
                        //if we don't have to check for a string that does not exist in the url
                        if ((legacyUrl.IndexOf(rule.OldUrlContains, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)
                            //so that it doesn't go in infinite loop since the end part of both urls are same
                            && (!(legacyUrl.IndexOf(rule.OldUrlContainsNot, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)))
                        {
                            newUrl = rule.NewUrl;
                        }
                    }
    
                    //found anything?
                    if (newUrl.Length > 0)
                    {
                        break;
                    }
    
                }
    
    
                if (newUrl.Length > 0)
                {
                    response.Status = status;
                    response.RedirectLocation = newUrl;
                    response.End();
                }
    
                return null;
            }
    
            public override VirtualPathData GetVirtualPath(RequestContext requestContext,
                        RouteValueDictionary values)
            {
                return null;
            }
        }
    

    Web.Config

    <system.webServer>
       <handlers>
          <add name="HtmlScriptHandler" path="*.html" verb="*"
             preCondition="integratedMode" type="System.Web.StaticFileHandler" />
        </handlers>
      </system.webServer>
    

    如果上面的配置处理程序没有写,那么当我们输入https://localhost/sample/page.html时浏览器会抛出404错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 2015-09-14
      • 2017-06-19
      • 2014-04-10
      • 2021-12-27
      • 2022-06-11
      相关资源
      最近更新 更多