Reputation: 4323
I have website that shows user submitted content in 30+ languages in front page,I am currently using paths like:
http://www.example.com?lang=en
or if it's not first page
http://www.example.com?lang=en&page=2
But that really isn't user or seo friendly. Is there a way in mvc to route these values to something like
http://www.example.com/en
and
http://www.example.com/en/2
without adding new action in-between like in this case lang
:
http://www.example.com/lang/en/2
Update. Here is what I came up from Alexeis answer, in case anybody will need same thing:
In case of just language:
routes.MapRoute("MyLang", "{lang}", new { controller = "Home", action = "Index" },new { lang = @"\D{2}"});
In case you need language and optionally page:
routes.MapRoute("MyLang", "{lang}/{page}", new { controller = "Home", action = "Index", page = UrlParameter.Optional }, new { lang = @"\D{2}", page = @"\d+"});
It should not catch any other paths unless you have Actions with only 2 letters.
Upvotes: 0
Views: 284
Reputation: 12705
u can add a new route like this
routes.MapRoute("MyLang", "{action}/{page}",
new { controller = "ControllerName",page=0 }
this way the lang name will automatically be mapped to the action and page will be passed as a parameter provided u have action signature as
public ActionResult English(int page)
obsly the return type and action name can be changed
Upvotes: 0
Reputation: 100547
You don't need "lang/" for the route to match culture. Simple "{lang}" will do as long as you order routes in a way so other routes are matched correctly. You may also consider constraints on routing parameters to limit number of conflicts.
routes.MapRoute("MyLang", "{lang}",
new { controller = "Home", action = "Home", }
class HomeController{
public ActionResult Home(string lang)
{
return View();
}
}
Upvotes: 1