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

发布于 2024-07-29 00:03:31 字数 1473 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄意 2024-08-05 00:03:31

如果你这样做的话应该会起作用:

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

因为这就是我自己所做的。

您到底是什么意思:Visual Studio 告诉您“会话在此上下文中不可用”? 您是否遇到编译器错误或运行时异常?

您可以尝试采取更多防御措施并测试是否确实存在当前的 HttpContext 和 Session:

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

It should work if you do it like this:

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

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:

if (HttpContext.Current != null &&
    HttpContext.Current.Session != null) {
  strError = HttpContext.Current.Session["trCustomerEmail"]
}
我们的影子 2024-08-05 00:03:31

我认为应用程序错误是特定于整个应用程序的,而会话是特定于用户的。 也许您可以抛出自己的异常,将会话中的信息保存在异常中。

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.

帅气尐潴 2024-08-05 00:03:31

你可以尝试这样:

HttpContext context = ((HttpApplication)sender).Context;

那么你必须使用这样的:

context.Request.QueryString.ToString()
context.Session["key"] = "fasfaasf";

但是如果在加载Session对象之前抛出异常,那么它将为null

You may try this:

HttpContext context = ((HttpApplication)sender).Context;

then you must use is like this:

context.Request.QueryString.ToString()
context.Session["key"] = "fasfaasf";

but if the exception was thrown before the Session object is loaded, it will be null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文