且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET:global.asax中的访问会话变量

更新时间:2023-09-21 10:22:58

如果这样做,它应该可以工作:

It should work if you do it like this:

strError = System.Web.HttpContext.Current.Session["trCustomerEmail"]

因为那是我自己做的事.

Because that is what I do myself.

您的确切意思是:Visual Studio告诉会话在此上下文中不可用"?您会收到编译器错误或运行时异常吗?

What exactly do you mean with: Visual Studio is telling that "Session is not available in this context"? Do you get a compiler error or a run-time exception?

您可以尝试更具防御性,并测试是否确实存在当前的HttpContext和一个Session:

You could try to be more defensive and test if there actually is a current HttpContext and a Session:

if (HttpContext.Current != null &&
    HttpContext.Current.Session != null) {
  strError = HttpContext.Current.Session["trCustomerEmail"]
}