Of course, there are many solutions. I am going to show you two:
- the one you control in your application
- which you control outside of your application (in IIS)
Solution - MVC MVC Routing
Provide routing that spans old and new routing:
routes.MapRoute( "New", "{lang}/{controller}/{action}/{id}", new { lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }, new { lang = "en|de|it|es|fr" } ); routes.MapRoute( "NewEx", "{lang}/{controller}/{action}/{id}/{title}", new { lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }, new { lang = "en|de|it|es|fr" } ); routes.MapRoute( "Old", "{controller}/{action}/{id}", new { lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( "OldEx", "{controller}/{action}/{id}/{title}", new { lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional } );
As you can see , I also specified the default language for the old routes , since it is not in the URL. Regardless of whether you want it or not, this is your own decision, but such routing allows you to not duplicate the actions of your controller. In any case, you will have to define a default language that can be provided this way.
There is another question, do you still want to support the old URLs, or do you prefer to redirect them (HTTP redirects to a constant status of 301).
Constant redirection
Change old routes to:
routes.MapRoute( "Old", "{controllerOld}/{actionOld}/{idOld}", new { controller = "Redirect", action = "Permanent", id = UrlParameter.Optional } ); routes.MapRoute( "OldEx", "{controllerOld}/{actionOld}/{idOld}/{titleOld}", new { controller = "Redirect", action = "Permanent", id = UrlParameter.Optional, title = UrlParameter.Optional } );
Then write a controller class that redirects:
public class RedirectController : Controller { public ActionResult Permanent(string controllerOld, string actionOld, string idOld, string titleOld) { return RedirectToRoutePermanent(new { lang = "en", controller = controllerOld, action = actionOld, id = idOld, title = titleOld }); } }
Solution Two - IIS URL Rewrite Module
This solution is based on the IIS URL rewriting module, where you can rewrite any request without choosing the default language.
I am not going to write how URL Rewriting works, because there are many web resources with detailed information about this.