View Complete Thread
  • Re: Response.Flush breaks IIS's "Custom Errors"

    Sep 14, 2006 03:37 PM|renniquer|LINK

    I've experienced the same error, and here's the workaround I came up with:

    1. Right after you call Response.Flush(), add this line of code:

    HttpContext.Current.Items["responseFlushed"] = true;

    2. In Global.asax, add the following to Application_Error:

    if (Convert.ToBoolean(HttpContext.Current.Items["responseFlushed"]))

    That should do it. You can add additional code to get your actual customErrors settings from web.config using System.Web.Configuration instead of hard coding the custom error page file path. You might also want to make sure Server isn't null.

    Note that you should use HttpContext.Current.Items and NOT HttpContext.Current.Session, as the Items collection will reset itself on each request, resetting Items["responseFlushed"] to false in the process.