您可以制作一个小型重定向控制器,并添加一个路由来匹配 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 控制器,并且如果需要,可能会更改路由以允许额外的参数。