0

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have adapted it a little bit : in the loop I add {lang} in place of language.LanguageAbbreviation), and remove default value (and add constraint for the "lang" parameter) for the route then it avoid to loop the languages, it works fine. Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.