更改 ASHX 处理程序上的 Thread.CurrentThread.CurrentCulture
我想更改网站的语言。 我以为我可以使用处理程序来完成此操作,因此下拉列表将指向 http:/ /domain.com/Handler.ashx?language=en-US, fi
因此,它调用具有以下代码的处理程序:
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());
但是当它返回时,Thread.CurrentThread.CurrentCulture 设置为 pt-BR,这是初始值。
我的问题是:处理程序上的线程与加载内容的aspx页面不同? 您建议采取什么解决方法?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Response.Redirect() 将 HTTP 重定向发送回用户的浏览器,然后浏览器向服务器发出另一个请求。 这会导致 IIS 处理一个全新的请求,因此会创建一个新线程来处理该请求。
虽然我不建议使用处理程序来完成此操作,但如果您切换到 Server.Transfer,您的想法可能会起作用,因为 Server.Transfer 不使用 Http 重定向,而只是创建一个新请求以通过 ASP.NET 管道发送,所有这些都在相同初始请求的上下文。
希望有帮助,
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,