Link to home
Start Free TrialLog in
Avatar of gerrie-govaerts
gerrie-govaertsFlag for Belgium

asked on

ASP.NET MVC - Changing Culture

Hello

I'm trying to change the displayed language in my MVC application by routing the ActionLink on my Masterpage to an action on a controller. However, I would like that the page I'm on refreshed itself with the changed culture.

Now I'm hardcoding to have a RedirectToAction back to the Index action of another controller.

Is is possible to return to the same page?

Here is my code:

    public class CultureController : Controller
    {
        public ActionResult ChangeLanguage(string culture)
        {
            HttpCookie cookie = new HttpCookie("StockManagementCookie");
            cookie.Values["culture"] = culture;
            cookie.Expires = DateTime.Now.AddYears(1);
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            return RedirectToAction("Index", "Reservation");
        }
    }
Avatar of Dhanasekaran Sengodan
Dhanasekaran Sengodan
Flag of India image

Avatar of gerrie-govaerts

ASKER

I'm sorry but both links do not refer to the problem I have talked about.

The problem is: instead of using hard coded RedirectToAction variables like Index + Reservation, I want to use the controller and action that have invoked the language change, so that I can redirect to it, as if the page has been refreshed, but now with another display language.
ASKER CERTIFIED SOLUTION
Avatar of rg20
rg20
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the comment rq20, it already gave me a good hint.

The actual way to do it in MVC is as follows:

                public ActionResult ChangeLanguage(string culture)
        {
            HttpCookie cookie = new HttpCookie("StockManagementCookie");
            cookie.Values["culture"] = culture;
            cookie.Expires = DateTime.Now.AddYears(1);
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            string returnUrl = Request.UrlReferrer.ToString();            return Redirect(returnUrl);
        }


Thx !!!
The comment gave me a good hint to search for.
Glad I could help

I have an MVC .NET question here, maybe you know the answer?

https://www.experts-exchange.com/questions/26427232/VS2010-MVC2.html?cid=1575&anchorAnswerId=33518074#a33518074