c# - 错误页面上的 ASP.net 捕获错误

标签 c# asp.net error-handling webforms http-error

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

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

关于c# - 错误页面上的 ASP.net 捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505968/

相关文章:

C# 属性继承无法正常工作

asp.net - 导出到excel丢失日期格式

asp.net - 第二个循环运行一次并停止

c# - Bot Framework v4.2 - 优雅地从 OnTurnError 异常中恢复

powershell - 如何处理Powershell高级功能ErrorAction参数?

python - 在Django中上传文件名

c# - 获取 Silverlight 5 部署路径(在浏览器中以完全信任模式运行时)

c# - 以编程方式创建 MSTest 单元测试

c# - 回发与 Javascript 客户端计算

c# - 获得最多数组的Linq解决方案