Вы можете создать небольшой контроллер перенаправления и добавить маршрут, соответствующий чему-то вроде 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 и, возможно, изменить маршрут, чтобы разрешить дополнительные параметры, если вам это требуется.