ASP.NET 4.0 Custom Error Pages Very Slow to Load

When I upgraded to ASP.NET 4.0, runtime errors suddenly became very slow. There was a consistent two-minute delay before my custom error page would appear. The rest of the website was lightning fast, so it was not related to compilation. I finally decided to tackle this problem, so I started copying blocks of code into a new, blank custom error page. The delay was not present until I copied this line of code:

Response.StatusCode = 500

I had been searching the web for terms like " error page very slow" with no success. One quick search on " statuscode slow" finally returned some helpful results:

All I had to do was drop in one line of code where I set the status code.

Response.TrySkipIisCustomErrors = True

The delay is gone now, but I still have no idea what's going on inside the black box.