且构网

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

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

更新时间:2023-09-21 10:31:52

如果你不喜欢这样它应该工作:

It should work if you do it like this:

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

由于这是我自己做的。

究竟你的意思是用什么: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和会话:

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"]
}