为了账号安全,请及时绑定邮箱和手机立即绑定

ASP.NET自定义错误页面 - Server.GetLastError()为null

ASP.NET自定义错误页面 - Server.GetLastError()为null

叮当猫咪 2019-08-16 16:56:42
ASP.NET自定义错误页面 - Server.GetLastError()为null我为我的应用程序设置了自定义错误页面:<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx"/>在Global.asax,Application_Error()中,以下代码用于获取异常详细信息:  Exception ex = Server.GetLastError();   if (ex != null)     {         if (ex.GetBaseException() != null)             ex = ex.GetBaseException();     }当我到达我的错误页面(〜/ errors / GeneralError.aspx.cs)时,Server.GetLastError()为null有没有什么办法可以在错误页面上获取异常详细信息,而不是在Global.asax.cs中?Vista / IIS7上的ASP.NET 3.5
查看完整描述

3 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

NailItDown和Victor所说的组合。首选/最简单的方法是使用Global.Asax存储错误,然后重定向到自定义错误页面。


Global.asax:


    void Application_Error(object sender, EventArgs e) 

{

    // Code that runs when an unhandled error occurs

    Exception ex = Server.GetLastError();

    Application["TheException"] = ex; //store the error for later

    Server.ClearError(); //clear the error so we can continue onwards

    Response.Redirect("~/myErrorPage.aspx"); //direct user to error page

}

此外,您需要设置web.config:


  <system.web>

    <customErrors mode="RemoteOnly" defaultRedirect="~/myErrorPage.aspx">

    </customErrors>

  </system.web>

最后,根据您存储在错误页面中的异常,执行您需要的任何操作:


protected void Page_Load(object sender, EventArgs e)

{


    // ... do stuff ...

    //we caught an exception in our Global.asax, do stuff with it.

    Exception caughtException = (Exception)Application["TheException"];

    //... do stuff ...

}


查看完整回答
反对 回复 2019-08-16
  • 3 回答
  • 0 关注
  • 890 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信