我有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);
}
我写了非页面封装类,在这个类中我需要从会话中获取此文化。我尝试这个班级得到这样的当前文化信息:
(CultureInfo)HttpContext.Current.Session["MyCulture"]
但是HttpContext.Current
是NULL!怎么解决这个???
答案 0 :(得分:1)
如果您的类没有从Page或UserControl继承,那么您应该将当前Context作为参数传递给需要使用它的方法。
此外,如果您的班级是ASHX页面,那么您需要指明该页面实现了System.Web.SessionState.IRequiresSessionState。
答案 1 :(得分:1)
我假设访问HttpContent.Current
的代码在一个特殊的线程上运行到ASP.NET线程。
如果是这种情况,则HttpContent.Current
将无法使用,因此最好将任何依赖对象(例如当前文化)传递到新线程,而不是依赖于访问权限到当前的HttpContext对象。