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 "asp.net error page very slow" with no success. One quick search on "asp.net statuscode slow" finally returned some helpful results:

http://stackoverflow.com/questions/3288797/page-not-rendered-when-sending-statuscode-500-to-the-client

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.tryskipiiscustomerrors.aspx

http://www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors

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.