ASP .NET MVC 5遗留网址

时间:2016-03-31 12:02:31

标签: c# asp.net-mvc routing

我必须将旧URL(从Classic Asp网站)重定向到使用ASP .NET MVC生成的新URL。例如:

localhost/news.asplocalhost/News

我加上以下规则:

   routes.MapRoute(
            "legacyUrl1",
            "{controller}.asp/{action}/{id}",
            new { controller = "News", action = "RedirectLegacyURL", id = UrlParameter.Optional } 
        );

//in news controller
 public void RedirectLegacyURL(string id)
    {
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", "news/" );
        Response.End();
    }

但规则"它使用"如下:

    routes.MapRoute(
            "Normal",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        );

这怎么可能?当我必须使用查询字符串重定向旧网址时,这会导致问题。

1 个答案:

答案 0 :(得分:2)

routes.Redirect(r => r.MapRoute("", "news.asp")).To(routes.Map("", "Home/Index", new { controller = "Home", action = "Index" }));

这就是我重定向旧的.asp路由的方式