在Global.asax中ASP.NET C#中的Application_Error无法访问会话变量 [英] ASP.NET C# Application_Error in Global.asax cannot access Session variables

查看:87
本文介绍了在Global.asax中ASP.NET C#中的Application_Error无法访问会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的错误处理的设置来跟踪所有的异常,写我们需要一个数据库中的信息,并通过电子邮件发送出去谁需要了解有关该错误,以解决它的开发者。当页面完全加载之前发生的错误,我遇到的问题是。在这种情况下,会话变量我想跟踪不在的Application_Error可用。所以,我这样做,这样我就不是我的错误处理得到一个错误,但必须有一个更好的办法。此外,如果在提交表单的出现页面加载和错误,则会话变量是可用的。

I have my error handling setup to track all exceptions and write the information we need to a database and email out the developers who need to know about the error in order to fix it. The problem I run into is when the error occurs before the page has fully loaded. In this case the session variables I want to track are not available in Application_Error. So I do the following so that I don't get an error in my error handling, but there has to be a better way. Also if the page loads and the error occurs on submission of the form then the session variables are available.

try
{
    user_name = System.Web.HttpContext.Current.Session["User_Name"].ToString();
    user_number = System.Convert.ToInt32(System.Web.HttpContext.Current.Session["User_Number"].ToString());
}
catch (Exception ex)
{
    user_number = 0; 
    user_name = "N/A";     
}



任何帮助是极大的赞赏。
谢谢!

Any help is greatly appreciated. Thanks!

推荐答案

有没有错,你在做什么,但一个

There's nothing wrong with what you're doing, although an

if ( null != System.Web.HttpContext.Current.Session )

会比你的try-catch块多外科手术。

would be more "surgical" than your try-catch block.

会议未初始化,直到AcquireRequestState事件添加,因此任何这点之前发生的错误将不会有会话变量。

Session is not initialized until the AcquireRequestState event, so any error occurring before this point will not have session variables available.

这篇关于在Global.asax中ASP.NET C#中的Application_Error无法访问会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆