ASP.NET MVC将旧URL重定向到新URL

时间:2017-09-05 11:20:12

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

我有一个包含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")

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

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

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

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

你能告诉我吗?

1 个答案:

答案 0 :(得分:0)

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

这是代码片段,与问题中给出的链接几乎相同。

RouteConfig.cs

$wo_request = "SELECT * from work_orders Where ID IN ('$ID')";

LegacyUrlRoute类:

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 }
      );

    }
}

的Web.Config

      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;
        }
    }

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