views:

334

answers:

1

Hi, I want to change the language in my website. I thought i could do it using a Handler, so the drop down would go for http://domain.com/Handler.ashx?language=en-US, f.i.

So, it calls the handler, that has this code:

string selectedLanguage = context.Request.QueryString["language"];

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

    context.Response.ContentType = "text/plain";
    context.Response.Write("Hello World");

    context.Response.Redirect(context.Request.UrlReferrer.AbsoluteUri.ToString());

But when it goes back, Thread.CurrentThread.CurrentCulture is set to pt-BR, which was the initial value.

My question is: the Thread on the Handler is different than the aspx page that loads the content? And what would you suggest as a work around?

Thank you

+1  A: 

Response.Redirect() sends an HTTP redirect back to the user's browser, the browser then makes another request to the server. This results in IIS handling an entirely new request and, therefore, a new thread is created to handle this request.

Although I would not recommend a handler to accomplish this, if you switch to Server.Transfer, your idea MAY work, as Server.Transfer does not use Http Redirects but simply creates a new request to send through the ASP.NET pipeline, all within the context of the same initial request.

Hope that helps,

LorenVS
Thank you. It worked as I wanted. Just out of curiosity: what would you recommend in this cases? ty again
EduardoMello
Well, localization and culture info should be something that you attempt to handle at a session level. Using this handler approach fixes the problem for all requests that run through this handler, but that puts unnecessary strain and confusion into your code. By implemented some really dirt simple server control or similar to place upon your pages (or in your master page) that OnInit sets the threads culture to some value stored in session, and you change that value when the user selects an item from the drop down, you've solved the problem for the entire session.I may not understand...
LorenVS
Okay. This solution works. But everytime i click a link, it simply 'restarts' the culture, i guess it is for the same reason you stated on the original answer. I'm going for the session level attempt.
EduardoMello