ASP.NET:访问 global.asax 中的会话变量
我有一个 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("[email protected]", "[email protected]", "Error in " + Request.Form.ToString(), strError, 2);
}
在访问会话变量的代码行中出现错误。 Visual Studio 告诉我们
会话在此上下文中不可用
我怎样才能摆脱这个问题?
I have an ASP.NET application and in the Global.asax ' Application Error Event, I am calling a method to trace/log the error. I want to use the session variable content here. I used the below code
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("[email protected]", "[email protected]", "Error in " + Request.Form.ToString(), strError, 2);
}
I am getting an error in the line of code where I am accessing the session variable. Visual Studio is telling that
Session is not available in this context
How can I get rid of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你这样做的话应该会起作用:
因为这就是我自己所做的。
您到底是什么意思:Visual Studio 告诉您“会话在此上下文中不可用”? 您是否遇到编译器错误或运行时异常?
您可以尝试采取更多防御措施并测试是否确实存在当前的 HttpContext 和 Session:
It should work if you do it like this:
Because that is what I do myself.
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?
You could try to be more defensive and test if there actually is a current HttpContext and a Session:
我认为应用程序错误是特定于整个应用程序的,而会话是特定于用户的。 也许您可以抛出自己的异常,将会话中的信息保存在异常中。
I think applicaiton error is specific for the whole application and a session is specific for the user. Maybe you can throw your own Exception where you save the information from the session inside your exception.
你可以尝试这样:
那么你必须使用这样的:
但是如果在加载Session对象之前抛出异常,那么它将为null
You may try this:
then you must use is like this:
but if the exception was thrown before the Session object is loaded, it will be null