asp net mvc url routing strategy and internationalization

跟風遠走 提交于 2020-01-17 04:43:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!