ASP.NET:global.asax中的Access Session变量

我有一个ASP.NET应用程序,在Global.asax’应用程序错误事件中,我正在调用一个跟踪/记录错误方法.我想在这里使用会话变量内容.我使用下面的代码
void Application_Error(object sender,EventArgs e)
{
     //get reference to the source of the exception chain
     Exception ex = Server.GetLastError().GetBaseException();

     //log the details of the exception and page state to the
     //Windows 2000 Event Log
     GUI.MailClass objMail = new GUI.MailClass();
     string strError = "MESSAGE: " + ex.Message + "<br><br><br>" + "SOURCE: " + ex.source + "<br>FORM: " + Request.Form.ToString() + "<br>QUERYSTRING: " +    Request.QueryString.ToString() + "<br>TARGETSITE: " + ex.TargetSite + "<br>STACKTRACE: " + ex.StackTrace;
     if (System.Web.HttpContext.Current.Session["trCustomerEmail"] != null)
     {
         strError = "Customer Email : " + Session["trCustomerEmail"].ToString() +"<br />"+ strError;
     }

     //Call a method to send the error details as an Email
     objMail.sendMail("test@gmail.com","myid@gmail.com","Error in " + Request.Form.ToString(),strError,2);   
}

我在访问会话变量的代码行中出现错误.Visual Studio告诉我

“Session is not available in this context”

如何摆脱这个?有什么想法吗?

提前致谢

解决方法

如果你这样做它应该工作:
strError = System.Web.HttpContext.Current.Session["trCustomerEmail"]

因为这就是我自己做的事情.

你的意思是什么:Visual Studio告诉“会话在这种情况下不可用”?您是否收到编译器错误或运行时异常?

如果确实存在当前的HttpContext和Session,您可以尝试更具防御性并进行测试:

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....