我有一个asp.net mvc3网站。它取代了一个旧的php网站。许多人都有部分网站的书签参考。php的位置,我想把这些添加回asp.net网站作为简单的转发到新的位置。因此mysite/product.php将重定向到mysite/usermap/product。例如CSHTML。当我将product.php插入目录并对其使用锚点href时,系统提示我使用某个程序打开它或保存它。什么好主意吗?
您可以制作一个小型重定向控制器,并添加一个路由来匹配mysite/{id}.php
。
然后在控制器
中public ActionResult Index(string id)
{
return RedirectToActionPermanent("Product", "YourExistingController", id);
}
编辑
在你的global.asax.cs文件
public void RegisterRoutes(RouteCollection routes)
{
// you likely already have this line
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// assuming you have a route like this for your existing controllers.
// I prefixed this route with "mysite/usermap" because you use that in your example in the question
routes.MapRoute(
"Default",
"mysite/usermap/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// route to match old urls
routes.MapRoute(
"OldUrls",
"mysite/{oldpath}.php",
new { Controller = "OldPathRedirection", action = "PerformRedirection", oldpath = "" }
);
}
那么你将定义一个OldPathRedirectionController
(最有可能是Controllers/OldPathRedirectionController.cs
)
public class OldPathRedirectionController : Controller
{
// probably shouldn't just have this hard coded here for production use.
// maps product.php -> ProductController, someotherfile.php -> HomeController.
private Dictionary<string, string> controllerMap = new Dictionary<string, string>()
{
{ "product", "Product" },
{ "someotherfile", "Home" }
};
// This will just call the Index action on the found controller.
public ActionResult PerformRedirection(string oldpath)
{
if (!string.IsNullOrEmpty(oldpath) && controllerMap.ContainsKey(oldpath))
{
return RedirectToActionPermanent("Index", controllerMap[oldpath]);
}
else
{
// this is an error state. oldpath wasn't in our map of old php files to new controllers
return HttpNotFoundResult();
}
}
}
我从最初的建议中清理了一点。希望这应该足以让你开始!明显的改变是不要硬编码php文件名到mvc控制器的映射,如果你需要的话,可能会改变路由以允许额外的参数。
如果您正在使用IIS7, Url重写模块是伟大的。页面如下:http://www.iis.net/download/urlrewrite
当我将product.php插入目录并对其使用锚点href时,系统提示我用某个程序打开它或保存它。什么好主意吗?
手动更新处理程序映射。但是我很确定当你为IIS (http://php.iis.net/)安装PHP时,它会为你做的。
使用此站点安装PHP到IIS。http://php.iis.net/