如何判断Application_Error中是否有Request / Response?

时间:2023-01-02 16:56:45

If Application_Error is triggered by an exception in the application start up i.e. RouteConfigor BundleConfig how can you check if the Request/Response is available? Currently the call to Response.Clear throws System.Web.HttpException with additional information Response is not available in this context.

如果Application_Error是由应用程序启动中的异常触发的,即RouteConfigor BundleConfig如何检查请求/响应是否可用?目前,对Response.Clear的调用会抛出System.Web.HttpException以及其他信息。在此上下文中,Response不可用。

void Application_Error(object sender, EventArgs e)
{
    //Log error
    Log.Error(e);

    //Clear
    Response.Clear();
    Server.ClearError();

    //Redirect
    Response.Redirect("~/Error");
}

Other questions suggest rewriting to not use Response or to use HttpContext.Current.Response or changing IIS config.

其他问题建议重写不使用Response或使用HttpContext.Current.Response或更改IIS配置。

In summary; how can I tell if the error has occurred during app start?

综上所述;如何判断应用程序启动期间是否出现错误?

1 个解决方案

#1


4  

You want to check whether it is HttpException.

你想检查它是否是HttpException。

protected void Application_Error(Object sender, EventArgs e)
{
    var exception = Server.GetLastError();

    // Log error
    LogException(exception);

    var httpException = exception as HttpException;
    if (httpException != null)
    {
        Response.Clear();
        Server.ClearError();
        Response.TrySkipIisCustomErrors = true;

        Response.Redirect("~/Error");
    }
}

#1


4  

You want to check whether it is HttpException.

你想检查它是否是HttpException。

protected void Application_Error(Object sender, EventArgs e)
{
    var exception = Server.GetLastError();

    // Log error
    LogException(exception);

    var httpException = exception as HttpException;
    if (httpException != null)
    {
        Response.Clear();
        Server.ClearError();
        Response.TrySkipIisCustomErrors = true;

        Response.Redirect("~/Error");
    }
}