In the default _Layout.cshtml file, which is the main layer, there are 2 buttons that switch the language of the site:

 <div class="language-flags"> <a href="@Url.Action("Change", "Language", new { LanguageAbbrevation = "en" }, null)"> <img src="@Url.Content("~/Resources/icons/en_flag.png")" title="English"/> </a> <a href="@Url.Action("Change", "Language", new { LanguageAbbrevation = "ru" }, null)"> <img src="@Url.Content("~/Resources/icons/ru_flag.png")" title="Русский"/> </a> </div> 

There is a LanguageController.cs :

 public class LanguageController : Controller { public ActionResult Change(string LanguageAbbrevation) { if (LanguageAbbrevation != null) { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation); Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation); } HttpCookie cookie = new HttpCookie("Language"); cookie.Value = LanguageAbbrevation; Response.Cookies.Add(cookie); return View(); // В этом и проблема } } 

There is also an empty Change view so that there are no errors.

The fact is that these buttons are displayed on each page of the site and after clicking, the user is sent to the Change page. You do not need to send the user anywhere, just update the current View. How to do it?

    1 answer 1

    You can pass in action an additional parameter URL to which you want to return:

     public ActionResult Change(string LanguageAbbrevation, string ReturnUrl) { // здесь все то же самое return Redirect(ReturnUrl); } 

    In _Layout specify the value of this additional parameter as Request.Url.PathAndQuery :

     <div class="language-flags"> <a href="@Url.Action("Change", "Language", new { LanguageAbbrevation = "en", ReturnUrl = Request.Url.PathAndQuery }, null)"> <img src="@Url.Content("~/Resources/icons/en_flag.png")" title="English" /> </a> <a href="@Url.Action("Change", "Language", new { LanguageAbbrevation = "ru", ReturnUrl = Request.Url.PathAndQuery }, null)"> <img src="@Url.Content("~/Resources/icons/ru_flag.png")" title="Русский" /> </a> </div>