问题
I have a website with urls like this : www.mysite.com/controller/action/id
I need to create urls for others languages (english), so I have used ALEX ADAMYAN implementation of the MultiCultureMvcRouteHandler. So now I have urls like this :
www.mysite.com/en/controller/action/id
www.mysite.com/fr/controller/action/id
but I need to keep my old urls has default one, ie to have :
www.mysite.com/en/controller/action/id
www.mysite.com/controller/action/id
So I'm wondering how I can modify the routes, in his implementation, alex loops the routes and add the en/fr parameter to all routes so it overwrites former routes, then former urls are dropped, what I want to avoid.
If I duplicate all the routes to keep one with fr/en and one without it doesn't works, may be this is because the order is lost in the route collection ?
回答1:
I solved this problem looping through all my languages:
public static List<Language> Languages;
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
LanguageRepository langRepo = new LanguageRepository();
Languages = langRepo.GetAllLanguages();
foreach (Language language in Languages)
{
routes.MapRoute(
"Localization_" + language.LanguageAbbreviation,
language.LanguageAbbreviation + "/{controller}/{action}/{id}",
new { lang = language.LanguageAbbreviation, controller = "Home", action = "Index", id = UrlParameter.Optional });
}
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {lang = "en", controller = "Home", action = "Index", id = UrlParameter.Optional });
}
Where langabbreviation likes "en" or "ru".
The last route is for "default" "withoutlanguage" url like "www.site.com/controller/action" and you need to set up the default lang abbr for it ("en" in my case).
Hope it will help.
来源:https://stackoverflow.com/questions/9103167/asp-net-mvc-url-routing-strategy-and-internationalization