设置文化会话 [英] Setting culture for session

查看:183
本文介绍了设置文化会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序会选择自己的国家后,它会被存储在cookie中,并存储起来用于以后的请求的每个用户。一切工作正常,但我需要设置文化在会话的开始。我目前在web.config中设置文化实验是EN-GB,然后使用在Global.asax覆盖会话的文化为en-US。 code以下

Each user of my application will choose their country, after which it will be stored in a cookie and stored for later requests. Everything is working OK, but I need to set the culture at the start of a session. I'm currently experimenting by setting the culture in the web.config to be en-GB and then using the Global.asax to override the culture for the session to en-US. Code below

protected void Session_Start(object sender, EventArgs e)
    {
        if (Globals.CountryID == 8)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
        }
    }

该countryID是8,区域性设置为en-US在以下code。但是,当我浏览一个网页,拥有的ToString(C)成立,它仍然显示英镑和文化仍然是EN-GB。

The countryID is 8, and the culture is set to en-US in the following code. However, when I navigate to a page that has ToString("C") set, it still displays in GBP and the culture is still en-GB.

有什么建议?

推荐答案

您的假设,将服务的页面请求的线程是已经开始会话在code在同一个线程 - 这是无法保证

You are assuming that the thread that will service the page request is the same thread that has started the session as in your code - this is not guaranteed.

您可能希望保存文化在会话变量和使用替代<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.page.initializeculture.aspx\"><$c$c>InitializeCulture在您的网页,如中所述:如何设置文化和UI文化为ASP.NET网页全球化

You may want to save the culture in a Session variable and use an override InitializeCulture in your pages, as described in: How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization.

这篇关于设置文化会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆