【问题标题】:Global.asax - Application_Error - How can I get Page data?Global.asax - Application_Error - 如何获取页面数据?
【发布时间】:2013-04-05 13:55:28
【问题描述】:

我有这个代码:

using System.Configuration;

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError().GetBaseException();

    string ErrorMessage = ex.Message;
    string StackTrace = ex.StackTrace;
    string ExceptionType = ex.GetType().FullName;
    string UserId = Getloggedinuser();
    string WebErrorSendEmail =
       ConfigurationManager.AppSettings["WebErrorSendEmail"];

    // save the exception in DB
    LogStuffInDbAndSendEmailFromDb();
}

这是(大部分)我的代码。 不过,在少数情况下,我没有得到足够的信息。我不知道异常来自哪个页面。

如何获取与异常源自的页面相关的任何类型的信息?

以下是最短消息的示例:

Base-64 字符数组的长度无效。

在 System.Convert.FromBase64String(String s) at System.Web.UI.ObjectStateFormatter.Deserialize(字符串输入字符串)在 System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(字符串 序列化状态)在 System.Web.UI.Util.DeserializeWithAssert(IStateFormatter 格式化程序, 字符串序列化状态)在 System.Web.UI.HiddenFieldPageStatePersister.Load()

【问题讨论】:

    标签: c# asp.net global-asax


    【解决方案1】:

    您可以像这样获取当前请求的 URL 和页面:

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        if (HttpContext.Current != null)
        {
            var url = HttpContext.Current.Request.Url;
            var page = HttpContext.Current.Handler as System.Web.UI.Page;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2011-04-12
      • 2012-12-07
      相关资源
      最近更新 更多