简体   繁体   English

如何通过查询字符串传递异常对象

[英]How to pass exception object through query string

void Application_Error(object sender, EventArgs e)
{   
    Exception objException = Server.GetLastError().GetBaseException();        
    Server.Transfer("~/ErrorPage/Error_Page.aspx?objException=" 
                        + objException,true);
}

//Error page.aspx page load
protected void Page_Load(object sender, EventArgs e)
{
    object objException = Request.QueryString["objException"];
    //Write exception in log file.
    WriteApplicationErrorLog(objException.Message, objException.StackTrace);
}

I am getting an exception object in Global.asax on application level. 我在应用程序级别的Global.asax获取异常对象。 I need to pass this exception object to a page errorpage.aspx , and write it to a log file. 我需要将此异常对象传递给页面errorpage.aspx ,并将其写入日志文件。

I am using the code above, but getting the exception message, and not the object. 我正在使用上面的代码,但收到异常消息,而不是对象。

You can make a class and pass the exception object to it to log it but if you want to pass it to error page any way then you can store exception object in session and access that in error page. 您可以创建一个类并将异常对象传递给它以进行记录,但是如果您想以任何方式将其传递给错误页面,则可以将异常对象存储在会话中并在错误页面中进行访问。

void Application_Error(object sender, EventArgs e)
{    
    Session["YourException"] =  Server.GetLastError().GetBaseException();
    Server.Transfer("~/ErrorPage/Error_Page.aspx?objException=" 
                        + objException,true);
}

protected void Page_Load(object sender, EventArgs e)
{
    Exception objException = (Exception ) Session["YourException"];

    //Write exception in log file
    WriteApplicationErrorLog(objException.Message, objException.StackTrace);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM