开发者

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

I have ASP.NET set up to use the CustomErrors functionality:

<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 nice, and the relevant error pages are shown when appropriate.

Except in the following two cases:

1) When there is an exception thrown in global.asax:

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

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

In both cases, I don't see my pretty 500.aspx page. Instead, I see the standard ASP.NET yellow screen of death, with the following message:

Server Error in '/MvcErrorHandling' Application.

Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the de开发者_StackOverflow中文版tails of the application error from being viewed.

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

Thanks in advance for any input :)


The problem is that those specific errors are happening before ASP.NET can load things up, and your "500 internal server error" page is an .aspx page that requires ASP.NET to load.

The simplest option would be to make your 500 page an HTML page, but that would mean you can't do simple error logging, etc from there.

This may still not help the web.config scenario as if IIS can't process the web.config, there's no guarantee that it would read your error section.

Another option would be to tell IIS to serve a static html page on 500 errors.

Finally you could try catching errors in the Application_Error event in the web.config - this would at least allow you to process the error, even if the page you try to display can't load up.


Edit to add

If you're running IIS 7 in integrated mode, you need to do one more thing if you're setting the response code of your error page to 500:

Response.TrySkipIisCustomErrors = true;

However, note that the following conditions will stop any custom 500 error page from displaying, resulting in the YSOD:

  1. Errors in the web.config or certain handlers in the web.config that fire before the rest of the pipeline (i.e. the bulk of the original answer)
  2. Errors in the error page.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜