.netiiswebservercustom-error-pages

IIS fail to serve custom error pages when .NET app fails on SQL connection


I would appreciate any help in this case.

I have a .NET Framework 4.5.1 web app running on IIS. I have two physical files in the root of this app:
Error404.html
Error500.html

In the web.config under "system.web" I created this configuration to serve custom error pages from .NET Framework:

<customErrors mode="RemoteOnly" defaultRedirect="/Error500.html" redirectMode="ResponseRewrite">
      <error statusCode="404" redirect="/Error404.html" />
      <error statusCode="500" redirect="/Error500.html" />
</customErrors>

In the web.config under "system.webServer" I created this configuration to serve custom error pages from IIS web server:

<httpErrors errorMode="DetailedLocalOnly" existingResponse="Auto">
  <remove statusCode="404" />
  <remove statusCode="500" /> 
  <error statusCode="404" path="/Error404.html" responseMode="ExecuteURL"/>
  <error statusCode="500" path="/Error500.html" responseMode="ExecuteURL"/>
</httpErrors>

My goal is to prevent to see detailed errors from internet (internet users). However I want to see detailed errors on my localhost.

So until this point, everything is working just fine. However when I change a connection string in my web.config to a host which doesn't exist (TCP Provider, error: 0 - No such host is known.), these rules for custom error pages are not working anymore.

From internet I get this error instead of my custom error page:

Server Error in '/' Application.
Runtime Error
Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

Why I can't see my custom error pages from internet?


Solution

  • Okay I think I understand now: I can't serve custom error pages because the whole app pool is broken. So .NET nor IIS as a web server can't do anything - no redirect, no execute url, no file content serving, nothing.... It's because the app pool itself isn't working. To make custom error pages work the application pool must be running. In my case the app pool is not running because during the initialization of the app pool there is the exception leading to a broken/not running app pool.

    The only way how to do it when I think about it right now is to use a reverse proxy server where I can set custom error pages. Then in this case when a backend isn't working I can serve custom error page from my reverse proxy server.
    Or I can use more resilient solution where I will implement a load balancer.