ASP.NET CustomErrors does not fire when exceptions occur in global.asax or in web.config

I have ASP.NET configured to use the CustomErrors function:

<customErrors mode="On" defaultRedirect="~/ErrorPages/500.aspx" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/ErrorPages/404.aspx" /> <error statusCode="500" redirect="~/ErrorPages/500.aspx" /> </customErrors> 

Everything works well, and the corresponding error pages are displayed when necessary.

Except for the following two cases:

1) If there is an exception in global.asax :

 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { throw new ApplicationException("Exception in Application_Start()"); } } 

2) If there is a syntax error in web.config

In both cases, I do not see my pretty 500.aspx page. Instead, I see a standard yellow ASP.NET death screen with the following message:

Server error in application / MvcErrorHandling.
Runtime Error Description. Application error on server. The current user error settings for this application do not allow viewing information about the application error.

How can I get ASP.NET or IIS to display a custom error page (pretty, not YSOD) in the two above scenarios?

Thanks in advance for any input :)

+4
source share
1 answer

The problem is that these specific errors occur before ASP.NET can load things, and your Internal Server Error page is the .aspx page for which ASP.NET needs to be loaded.

The easiest option is to make your page 500 HTML pages, but that means you cannot make a simple error log, etc. from there.

This may still not help the web.config script, as if IIS could not process the web.config file, there is no guarantee that it will read your error section.

Another option is to tell IIS to serve a static html page with 500 errors.

Finally, you can try to catch errors in the Application_Error event in the web.config file - this will at least allow you to handle the error even if the page you are trying to display cannot load.


Edit to add

If you are running IIS 7 in integrated mode, you need to do one more thing if you set the response code on the error page to 500:

 Response.TrySkipIisCustomErrors = true; 

However, note that the following conditions will stop the user from displaying 500 error pages, resulting in YSOD:

  • Errors in the web.config file or some handlers in the web.config file that run before the rest of the pipeline (i.e. the main part of the original response)
  • Errors on the error page.
+1
source

All Articles