MysticEarth
MysticEarth

Reputation: 2806

ASP MVC 2 Change language by url

I'm trying to create a languageswitch for a website with 2 or more languages.

When I follow the default route (controller/action) the Html.ActionLink("NL", ViewContext.RouteData.Values["action"].ToString(), new { language = "nl-NL" }, null) does it's work. But when called from an Action with parameters, it (logically) only creates a link to the Controller with the current Action. The parameters are ignored.

My current route:

routes.MapRoute(
            "ProjectCategory",
            "{language}/Projects/{action}/{slug}",
            new { controller = "Projects", action = "Detail", slug = string.Empty, language = "en-US" }
        );

The link created with Html.ActionLink:

http://localhost/mysite/nl-NL/Projects/Detail/

How to solve this problem?

Upvotes: 0

Views: 1180

Answers (1)

Eduardo Molteni
Eduardo Molteni

Reputation: 39413

 HttpContext.Current.Request.Path.Replace("/en-US/", "/nl-NL/")

It's not the most elegant way, but works for me. (Of course you should replace en-US with the current lang)

Upvotes: 1

Related Questions