Global.asax - Application_Error - How can I get Page data?

走远了吗. 提交于 2019-12-12 07:30:07

问题


I have this code:

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();
}

This is (most of) my code. In a small percentage of cases, I don't get enough information though. I don't know what page the exception originated from.

How can I get any kind of information related to the page that the exception originated from?

Below is an example of the shortest message:

Invalid length for a Base-64 char array.

at System.Convert.FromBase64String(String s) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) at System.Web.UI.HiddenFieldPageStatePersister.Load()


回答1:


You can get the current request's URL and page like this :

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;
    }
}


来源:https://stackoverflow.com/questions/15835802/global-asax-application-error-how-can-i-get-page-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!