If Application_Error
is triggered by an exception in the application start up i.e. RouteConfig
or 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
.
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.
In summary; how can I tell if the error has occurred during app start?
You want to check whether it is 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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With