ASP.net 在错误页面捕获错误

ASP.net catch error on error page

我已经设置了我的自定义错误页面,它工作正常:

<customErrors mode="On"  redirectMode="ResponseRewrite">
  <error statusCode="500" redirect="~/Pages/ErrorPages/500.aspx" />
</customErrors>

500.aspx:

    Response.ClearContent();
    Response.StatusCode = 500;
    Exception = Server.GetLastError();
    Code.Errors.Error.RecordNewError(ErrorType.InternalException, Exception, Code.Common.GetUserIPAddress().ToString(), HttpContext.Current);
    Server.ClearError();

问题是,如果错误页面本身抛出错误,我们会得到一个难看的错误:

Runtime Error

Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

如果错误页面本身抛出错误,我如何返回到更基本的自定义错误页面?

刚刚想通了!在错误页面本身上使用 Page_Error 方法:

    private void Page_Error(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.StatusCode = 500;
        Exception = Server.GetLastError();
        HttpContext.Current.Response.Write("Error");
        HttpContext.Current.Response.End();
    }