繁体   English   中英

如何将错误消息从一个aspx页面发送到另一aspx页面?

[英]How to send error message from one aspx page to another aspx page?

protected void Application_Error(object sender, EventArgs e)     
{
       Exception ex = this.Server.GetLastError();   
       this.Server.ClearError();    
       string errorMessage = ex.Message;   
       logger.Error(errorMessage, ex);    
       Response.Redirect("~/Error.aspx");
 }

在这种情况下,我希望参加会议。 然后,您可以保留包括堆栈跟踪在内的完整异常,以进行进一步处理。 但是您不能直接在Application_Error访问会话。 这应该工作:

private void Application_Error(object sender, EventArgs e)
{
    Exception ex = this.Server.GetLastError();   
    // ...
    HttpApplication application = (HttpApplication)sender;
    HttpContext context = application.Context;
    context.Session["LastError"] = ex;
    Response.Redirect("~/Error.aspx");
}

现在,您可以通过以下方式访问Error.aspx中的异常:

protected void Page_Load(Object sender, EventArgs e)
{
    Exception ex = (Exception)Session["LastError"];
}

我认为您可以在Global.asax使用Server.Transfer

void Application_Error(object sender, EventArgs e)
  {
                    // Code that runs when an unhandled error occurs

                    //direct user to error page 
                    Server.Transfer("~/Error_Pages/ErrorPage500.aspx");
  }

在错误页面中,您可以获取内部异常以检查实际发生的异常:

protected void Page_Load(object sender, EventArgs e)
  {
                LoadError(Server.GetLastError());
  }

protected void LoadError(Exception objError)
  {
                Exception innerException = null;           
                if (objError != null)
                {
                    if (objError.InnerException != null)
                    {
                        innerException = objError.InnerException;
                    }
                }
 }

暂无
暂无

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

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