【问题标题】:Handling legacy url's for any level in MVC处理 MVC 中任何级别的遗留 url
【发布时间】:2011-02-13 20:52:33
【问题描述】:

我已经将一个旧的 php 站点升级到 asp.net MVC,并且我想处理所有旧 URL。我不需要它们映射到当前页面,因为内容是全新的,但我确实想捕获对 .php 页面的所有请求并将它们发送到主页(理想情况下为搜索引擎提供永久重定向)。

这有效,但仅在根级别:

 routes.MapRoute(
             "LegacySite",                                                    // Route name
             "{OldPage}.php",                           // URL with parameters
             new { controller = "Home", action = "index" },  // Parameter defaults
             new string[] { "EatIn.Website.Controllers" }
        );

我想获取所有 .php 页面,无论它们位于哪个文件夹中。

【问题讨论】:

  • 威廉,很高兴这对你有用 - 谢谢你的“锣”;)

标签: asp.net-mvc


【解决方案1】:

威廉,

我在一个旧的 mvc 1 网站上有一个类似的场景。但是,逻辑可能仍然合适。我有许多已知页面和各种未知页面要处理。当时,我在行动中使用了一种相当笨拙的方法来处理它,如下所述:

public ActionResult Index()
{
    string routeval = (string)this.RouteData.Values["file"];
    var dicT = new Dictionary<string, int>();
    // oh deary me - hardcoded values
    dicT.Add("algarve", 1);
    dicT.Add("brazil", 5);
    dicT.Add("colorado", 4);
    dicT.Add("morocco", 2);
    dicT.Add("thailand", 6);
    // what, no test for culture/case :-)
    if (dicT.ContainsKey(routeval))
    {
        var routeData = new RouteValueDictionary(
            new
            {
                controller = "Property",
                action = "Details",
                id = dicT[routeval],
                Lang = "en"
            });
        return RedirectToAction("Details", "Property", routeData);
    }
    else
    {
        return View();
    }
}

[编辑] - 从那个旧的 mvc1 应用程序中找到另一个“有用”的信息。您可以将以下内容添加到基本控制器:

public class LegacyUrlRoute : RouteBase
{
    // source: http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC
    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 = "";

        if (legacyUrl.Contains("/villas/") || legacyUrl.EndsWith("asp"))
        {
            if (legacyUrl.Contains("/villas/"))
                newUrl = "/en/home/Destinations";
            else if (legacyUrl.EndsWith("asp"))
            {
                newUrl = "/en/home/index";
                if (legacyUrl.Contains("about"))
                    newUrl = "/en/home/AboutUs";
                if (legacyUrl.Contains("offers"))
                    newUrl = "/en/home/Offers";
                if (legacyUrl.Contains("contact"))
                    newUrl = "/en/home/ContactUs";
                if (legacyUrl.Contains("destinations"))
                    newUrl = "/en/home/destinations";
                if (legacyUrl.Contains("faq"))
                    newUrl = "/en/home/Faq";
                if (legacyUrl.Contains("terms"))
                    newUrl = "/en/home/Terms";
                if (legacyUrl.Contains("privacy"))
                    newUrl = "/en/home/Privacy";
            }

            response.Status = status;
            response.RedirectLocation = newUrl;
            response.End();
        }
        return null;
    }
    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        return null;
    }
}

然后在您的 global.asax 文件中添加一条新路由:

routes.Add(new LegacyUrlRoute());

基本上,我“知道”我正在寻找一个包含“文件”参数的路由值,类似于www.url.com/somepage.asp?file=algarve 等。(这就是旧的 asp 经典网站的工作方式,我知道目前没有这个)。对于我想要重定向到的“已知”路由,我使用了执行RedirectToAction 的逻辑,其他一切都通过默认的Index() View()

正如我所说,笨拙而不是我现在(大约 2.5 年后)会怎么做,但这是我“四处飘荡”的一个例子,它确实有效! :D

祝你好运

【讨论】:

    【解决方案2】:

    看看 Phil Haack 的 RouteMagic - 也许它会对你有所帮助。

    这是一个描述路由重定向的博客条目: http://haacked.com/archive/2011/02/02/redirecting-routes-to-maintain-persistent-urls.aspx

    【讨论】:

    • 我需要将路由永久重定向,但这似乎不支持。
    • 它确实:public static RedirectRoute Redirect(this RouteCollection routes, Func routeMapping, bool Permanent = false)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2011-08-02
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多