处理遗留的URL在MVC任何级别 [英] Handling legacy url's for any level in MVC

查看:95
本文介绍了处理遗留的URL在MVC任何级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经升级旧的PHP网站ASP.NET MVC和我要处理所有遗留的URL。我不需要它们映射到当前网页的内容都是新的,但我也想搭上PHP页面的所有请求,并将它们发送到首页(最好用的搜索引擎永久重定向)。

I have upgraded an old php site to asp.net MVC and I want to handle all the legacy URL's. I don't need them to map to the current pages as the content is all new but I do want to catch all requests for .php page and send them to the homepage (ideally with a permanent redirect for the search engines).

这工作,但只能在根级别:

This works but only at the root level:

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

我想所有的.php页,无论他们是在文件夹中。

I want to get all .php pages regardless of the folder they were in.

推荐答案

威廉,

我有一个古老的MVC 1网站类似的情景。然而,逻辑可能仍然适合。我有一些知名的网页和各种不知名的页面来处理。在那个时候,我处理了它在动作用,而cludgy方法具体如下:

I've got a similar scenario in an old mvc 1 website. However, the logic may still fit. I had a number of known pages and a variety of unknown pages to deal with. at the time, I dealt with it in the action using a rather cludgy approach as detailed below:

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应用程序。您可以添加以下到你的基地控制器:

[edit] - found another 'useful' bit of info on this from that old mvc1 app. you can add the following to your base controller:

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文件:

and then put a new route in your global.asax file:

routes.Add(new LegacyUrlRoute());

基本上,我'知道',我一直在寻找的是,包含沿着 www.url.com/somepage.asp?file=algarve 等。(这是旧的ASP经典的网站是如何工作的,我知道的present一个也没有这个)。对于已知的路​​线,我想重定向到,我用做了 RedirectToAction ,一切只是通过默认指数走到逻辑( )查看()

basically, i 'knew' that i was looking for a route value that contained a parameter of 'file' along the lines of www.url.com/somepage.asp?file=algarve etc.. (this was how the old asp classic site worked and i knew that the present one didn't have this). For the 'known' routes that i wanted to redirect to, i used the logic that did the RedirectToAction, everything else just went via the default Index() View().

正如我所说,cludgy而不是neccesarily我怎么会做,现在(约250年后),但它是我有'漂浮'的例子,它的实际工作! :D

As i said, cludgy and not neccesarily how i'd do it now (some 2.5 years later) but it was an example that i had 'floating around' and it actually works!! :D

好运气

这篇关于处理遗留的URL在MVC任何级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆