I have started converting my simple website to ASP.NET MVC, just to mess around with it. I have a switch language feature on there that basically sets the Session["language"] to another language and refreshes the page. Please correct me if this could be done better, but I have made two controllers for this and setting the session in there. The problem is the routing at the end. Could I refresh the page in some neat way, or can I get the current Action and re-route it to that? Or is this more a scenario for Ajax?

Thankful for advice!

Was it helpful?


is there any reason why you are using the session variable? a more common solution is to include the language code in the route, i.e. or (for english and japanese)

if you did this every page on the site could contain links to each language. if you are writing a publicly accessible site this would also make it easier for google to index all your content.

this article explains how to include the language in the domain, ie. or

UPDATED: Here's a simple example of including the language code in the URL route.

Change the default route to include a language parameter:

new { language = "en", controller = "Home", action = "Index", id = "" }

Add links for each language to your masterpage:

<li><%= Html.ActionLink(
    new { language = "es" })%></li>
<li><%= Html.ActionLink(
    new { language = "fr" })%></li>
<li><%= Html.ActionLink(
    new { language = "en" })%></li>    

These will render as links back to the page you are on - only with the language changed.


Following approach works good for me:

I'm using cookies and my own engine for localization All you need to put some link on the page that will redirect to something like this:

public class LanguageController : Controller
    // GET: /Language/

    public void Change(string id)
        var cuka = new HttpCookie("lang", id + "");
        cuka.Expires = DateTime.Now.AddYears(10);

        if (Request.UrlReferrer.IsNotNull())



If you're interested in this engine, let me know.

Here is simple solution how to enable selecting different in URL.

there is a controller for the language management

    public class LocalesController : Controller

    public ActionResult Index(string lang = "en_US")
        Response.Cookies["CacheLang"].Value = lang;

        if (Request.UrlReferrer != null)

        var message = Localization.Get("changedlng");

        return Content(message);


you can call it separately

new LocalesController().Index("fa");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow