Home > DeveloperSection > Forums > How can I tell if Request/Response is available in Application_Error?
Sanjay Sharma
Sanjay Sharma

Total Post:45

Points:315
Posted on    June-01-2015 1:00 AM

 .NET C#  ASP.Net  Error 
Ratings:


 1 Reply(s)
 980  View(s)
Rate this:

If Application_Error is triggered by an exception in the application. RouteConfigor BundleConfig.

how can you check if the Request/Response is available?


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");

}

 



Jennifer Morgan
Jennifer Morgan

Total Post:13

Points:95
Posted on    June-01-2015 1:17 AM

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");

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick