title: Display only the language on the URL of an MVC website
body: <p>I have a multilingual project developed with MVC 5, it also has routing. The URL is displayed as follows: example.com/en-US, but now I need the URL appears as follows: example.com/en (without '-US') or example.com/es</p>
  
  <p>RouteConfig.cs File:</p>
  
  <pre><code>using System.Web.Mvc;
  using System.Web.Routing;
  
  namespace pascual_mvc
  {
  public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapLocalizeRoute("Default",
              url: "{culture}/{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
  
              constraints: new { culture = "[a-zA-Z]{2}-[a-zA-Z]{2}" });
  
  
          routes.MapRouteToLocalizeRedirect("RedirectToLocalize",
                      url: "{controller}/{action}/{id}",
                      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
      }
  }
  }
  </code></pre>
  
  <p>_Layout.cshtml File:</p>
  
  <pre><code>&lt;ul&gt;                   
  &lt;li&gt;@Html.ActionLink("Español", "ChangeLang", "Home", new { Lang = "es-ES", ReturnUrl = Url.Encode(Request.RawUrl) }, new { @class = "", @style = "" })&lt;/li&gt;
  &lt;li&gt;@Html.ActionLink("English", "ChangeLang", "Home", new { Lang = "en-US", ReturnUrl = Url.Encode(Request.RawUrl) }, new { @class = "", @style = "" })&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>LocalizedRouteHandler.cs File:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Routing;
  
  namespace pascual_mvc
  {
  public class LocalizedRouteHandler : MvcRouteHandler
  {
      protected override System.Web.IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
      {
          var urlLocale = requestContext.RouteData.Values["culture"] as string;
          var cultureName = urlLocale ?? "";
  
          var cookieLocale = requestContext.HttpContext.Request.Cookies["locale"];
          if (cookieLocale != null)
          {
              // if request contains locale cookie, we need to put higher priority than url locale
              // user might click the link from somewhere but he/she already set different locale
              if (!cookieLocale.Value.Equals(urlLocale, StringComparison.OrdinalIgnoreCase))
              {
                  // if cookie locale and url cookie are different,
                  // we should redirect with cookie locale
                  var routeValues = requestContext.RouteData.Values;
                  routeValues["culture"] = cookieLocale.Value;
  
                  var queryString = requestContext.HttpContext.Request.QueryString;
                  foreach (var key in queryString.AllKeys)
                  {
                      if (!routeValues.ContainsKey(key))
                      {
                          routeValues.Add(key, queryString[key]);
                      }
                  }
  
                  return new RedirectHandler(new UrlHelper(requestContext).RouteUrl(routeValues));
              }
              else
              {
                  cultureName = cookieLocale.Value;
              }
          }
  
          if (cultureName == "")
          {
              return GetDefaultLocaleRedirectHandler(requestContext);
          }
  
          try
          {
              var culture = CultureInfo.GetCultureInfo(cultureName);
              Thread.CurrentThread.CurrentCulture = culture;
              Thread.CurrentThread.CurrentUICulture = culture;
          }
          catch (CultureNotFoundException)
          {
              // if CultureInfo.GetCultureInfo throws exception
              // we should redirect with default locale
              return GetDefaultLocaleRedirectHandler(requestContext);
          }
  
          if (cookieLocale == null)
          {
              requestContext.HttpContext.Response.AppendCookie(new HttpCookie("locale", cultureName));
          }
          return base.GetHttpHandler(requestContext);
      }
  
      private static IHttpHandler GetDefaultLocaleRedirectHandler(RequestContext requestContext)
      {
          var uiCulture = CultureInfo.CurrentUICulture;
          var routeValues = requestContext.RouteData.Values;
          routeValues["culture"] = uiCulture.Name;
          return new RedirectHandler(new UrlHelper(requestContext).RouteUrl(routeValues));
      }
  }
  }
  </code></pre>
  
  <p>How should I change those codes?</p>
  
  <p>I downloaded the project from this site: <a href="http://www.jittuu.com/2014/3/17/AspNet-localization-routing/" rel="nofollow">http://www.jittuu.com/2014/3/17/AspNet-localization-routing/</a> </p>