I have an asp.net mvc3 website. It replaced an older php website. Many people have parts of the site bookmarked in reference to the .php locations and I would like to add those back into the asp.net site as simple forwards to the new location. So mysite/product.php would redirect to mysite/usermap/product.cshtml for example. When I insert the product.php into the directory and use an anchor href to it, I am prompted to open it with a certain program or save it. Any ideas?
问题:
回答1:
You could make a small redirection controller, and add a route to match something like mysite/{id}.php
.
Then in that controller
public ActionResult Index(string id)
{
return RedirectToActionPermanent("Product", "YourExistingController", id);
}
edit
In your global.asax.cs file
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 = "" }
);
}
Then you would define an OldPathRedirectionController
(Controllers/OldPathRedirectionController.cs
most likely)
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();
}
}
}
I cleaned that up a little from the original recommendation. That hopefully should be enough to get you started! Obvious changes are to not hardcode the map of php filenames to mvc controllers, and perhaps altering the route to allow extra params if you require that.
回答2:
If you are using IIS7 the Url rewrite module is great. Here is the page: http://www.iis.net/download/urlrewrite
When I insert the product.php into the directory and use an anchor href to it, I am prompted to open it with a certain program or save it. Any ideas?
Update the handler mappings manually. However I am pretty sure when you install PHP for IIS (http://php.iis.net/) it will do it for you.
回答3:
Install PHP into IIS using this site. http://php.iis.net/