title: Setting route value of main url from a partial view
body: <p>I'm working on a multi-language ASP.NET MVC site, and I need some help setting the language in the main action from a partial view.</p>
  
  <p>My default route is declared as:</p>
  
  <pre><code>routes.MapRoute(
              name: "Default",
              url: "{language}/{controller}/{action}/{id}",
              defaults: new { language = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
  </code></pre>
  
  <p>In my _Layout.cshtml, I've declared a partial reference as:</p>
  
  <pre><code>@Html.Partial("_LanguagePartial")
  </code></pre>
  
  <p>This Partial invokes an Action in the Language Controller:</p>
  
  <pre><code>@Html.Action("Index","Language")
  </code></pre>
  
  <p>This controller action just pumps the released languages to the view:</p>
  
  <pre><code>public class LanguageController : BaseController
  {
      //
      // GET: /Language/
      public PartialViewResult Index()
      {
          List&lt;Language&gt; languages = new List&lt;Language&gt;();
          foreach (Language language in Global.Data.Languages.Values)
          {
              if (language.IsReleased)
                  languages.Add(language);
          }
          return PartialView(languages);
      }
  
      public ActionResult SelectLanguage(string code)
      {
          //I tried to change the route value here, but this only affects the route to this action
          var routeDataDict = HttpContext.Request.RequestContext.RouteData.Values;
          routeDataDict["language"] = code;
          return RedirectToRoute(routeDataDict);
      }
  }
  </code></pre>
  
  <p>The actual view that is shown in the partial, looks like this:</p>
  
  <pre><code>&lt;div class="float-center"&gt;
  &lt;table&gt;&lt;tr&gt;
  @foreach (var language in Model) {
      &lt;td&gt;
      &lt;a href="@Url.Action("SelectLanguage", new { code = language.CultureCode })"&gt;
          &lt;!--The css for the correct sprite is generated by language.Icon.ToString()
              The result will look like "background: url("/Images/Icons.png") -0px -100px; display: block; width: 32px; height: 20px; text-indent: -9999px;"--&gt;
          &lt;img alt="" src="" style="@Url.Content(@language.Icon.ToString());border=1" /&gt;
      &lt;/a&gt;
      &lt;/td&gt;
  }
  &lt;/tr&gt;&lt;/table&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now this partial is rendered in every page of the site, like it should, but when I'm for example on mydomain/en/SomeController/SomeAction and I click on let's say the German icon, it should take me to mydomain/de/SomeController/SomeAction.</p>
  
  <p>Off course the action and controller are not fixed.</p>
  
  <p>Any ideas on how I can perform such an action?
  Maybe I've implemented the partial wrong and I'll be able to access the route if I implement it differently.</p>
  
  <p><strong>EDIT:</strong>
  With the help of meilke's answer, I've made following edit to the Language controller:</p>
  
  <pre><code>public ActionResult SelectLanguage(string code)
      {
          string url = ReplaceLanguageCode(Request.UrlReferrer.ToString(), code);
          return Redirect(url);
      }
  
      private string ReplaceLanguageCode(string url, string code)
      {
          Language language = GetLanguage(url);
          if (language == null)
          {
              //No Language is set in the url e.g. http://mydomain/
              string domain = Request.Url.AbsoluteUri.ToString().Replace(Request.Url.PathAndQuery, "");
              return url.Replace(domain, domain + "/" + code);
          }
          else
              //A Language was already set in the url e.g. http://mydomain/en/
              return url.Replace("/" + language.CultureCode + "/", "/" + code + "/");
      }
  
      private Language GetLanguage(string url)
      {
          //Loop the apps languages to see if one is set in the url
          foreach (Language language in Global.Data.Languages.Values)
          {
              if (url.ToLower().Contains("/" + language.CultureCode + "/"))
                  return language;
          }
          return null;
      }
  </code></pre>
  
  <p>The SetLanguage action is changed to use the UrlReferrer as stated by meilke's answer. I also added ReplaceLanguageCode to create the new url and GetLanguage to get the language if the original url contains one. </p>