Search code examples
iishttpexceptionrequest-timed-out

Why does IIS return a 500 status code when the server times out?


We're running IIS7 & .net 4.0. When the server times out due to a long running request, the error page is shown, but the error code is just 500 rather than 408 or possibly 503 that I'm expecting. We want to show a different error page if it's a timeout, but I can't configure this in the <customErrors> section if it's just giving a 500 error. Is this a configuration issue?


Solution

  • You can add code like this into your global.asax.cs

    public class Global : System.Web.HttpApplication
    {
    protected void Application_Error(Object sender, EventArgs e)
    {
        Exception ex = HttpContext.Current.Server.GetLastError();
        if (ex != null && ex is HttpException && ex.Message == "Request timed out.")
        {
           HttpContext.Current.Response.StatusCode = 503;
           HttpContext.Current.Response.End();
        }
    }
    }
    

    I found this to not work properly and still return a 500 error without the Response.End() in there. Given your question, I'm not sure if you want to do a redirect instead to show an error page that would itself output the 503 instead of in the above.

    Its really ASP.NET returning the 500 status, IIS is just passing it along.