如何在包装类中获取当前的CultureInfo?
我有 Main.Master 页面,其中包含设置 CultureInfo 并将其存储在会话中的按钮:
protected void RU_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("ru-Ru");
Server.Transfer(Request.Url.LocalPath);
}
protected void USA_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("en-AU");
Server.Transfer(Request.Url.LocalPath);
}
我在包装器类后面编写了非页面,并且在此类中我需要从会话中获取此文化。我怎样才能得到它?
I have Main.Master page with buttons that set CultureInfo and store it in session:
protected void RU_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("ru-Ru");
Server.Transfer(Request.Url.LocalPath);
}
protected void USA_Click(object sender, ImageClickEventArgs e)
{
Session["MyCulture"] = CultureInfo.CreateSpecificCulture("en-AU");
Server.Transfer(Request.Url.LocalPath);
}
I write I non page behind wrapper class and in this class I need to get this Culture from session. How can I get it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用文化和本地化信息时最好的做法是将其设置到它们所属的位置:在当前线程上。试试这个:
通过这种方式,您始终可以从运行请求的线程中获取当前区域性。您还可以考虑设置 CurrentUICulture 以及一些使用该值的本地化机制。
The best thing to do when working with Culture and localization information is to set it to where they belong: on the current thread. Try this:
This way you can always get the current culture from the thread your request is running on. You may also consider setting CurrentUICulture as well as some of the localization mechanism use that value.
要遵循当前的方法,如果您只想访问不继承自 System.Web.UI.Page 的类中的会话变量,您可以像这样简单地访问它。
我假设您添加了一个单独的类。
如果您有一个 Web 项目,您可以尝试将其集成到 Global.asax 中,并让您的包装类也包装 Global.asax 属性。
To follow your current approach if you just want to access a session variable in a class that doesnt inherit from System.Web.UI.Page you can simply access it like this.
I'm making the assumption that you added a seperate class.
If you have a web project you could try to integrate this in your Global.asax and have your wrapper class wrap the Global.asax properties too.