我已将旧的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页面,无论它们在哪个文件夹中。
答案 0 :(得分:4)
威廉,
我在旧的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();
}
}
[edit] - 从旧的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
祝你好运答案 1 :(得分:0)
看看Phil Haack的 RouteMagic - 也许它会对你有帮助。
这是描述路由重定向的博客条目: http://haacked.com/archive/2011/02/02/redirecting-routes-to-maintain-persistent-urls.aspx