当global.asax或web.config中发生异常时,ASP.NET CustomErrors不会触发 [英] ASP.NET CustomErrors not firing when exceptions occur in global.asax or in web.config

查看:94
本文介绍了当global.asax或web.config中发生异常时,ASP.NET CustomErrors不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ASP.NET设置为使用CustomErrors功能:

 < customErrors mode =OndefaultRedirect = 〜/ ErrorPages / 500.aspxredirectMode =ResponseRewrite> 
< error statusCode =404redirect =〜/ ErrorPages / 404.aspx/>
< error statusCode =500redirect =〜/ ErrorPages / 500.aspx/>
< / customErrors>

一切都很好,适当时显示相关的错误页面。



除了以下两种情况之外:



1)当 global.asax

  public class MvcApplication:System.Web.HttpApplication 
{
protected void Application_Start()
{
抛出新的ApplicationException(Application_Start()中的异常);
}
}

2)当$中有语法错误c $ c> web.config



在这两种情况下,我看不到我漂亮的 500.aspx 页面。相反,我看到标准的ASP.NET黄色屏幕死亡,并显示以下消息:


'/ MvcErrorHandling'应用程序中的服务器错误。

运行时错误

说明:服务器上发生应用程序错误。此应用程序的当前自定义错误设置会阻止查看应用程序错误的详细信息。


如何获取ASP.NET或在上述两种情况下,IIS显示一个自定义错误页面(一个漂亮的,而不是YSOD)?



提前感谢任何输入:)

解决方案

问题是在ASP.NET可以加载内容之前发生这些具体错误,而您的500内部服务器错误页面是.aspx页面,需要ASP.NET加载。



最简单的选择是让您的500页面成为HTML页面,但这意味着您不能简单错误日志记录等等。



这可能仍然无法帮助web.config方案,如果IIS无法处理web.config,不能保证它会阅读您的错误部分。



另一个选项是告诉IIS为500个错误提供静态HTML页面。



最后,您可以尝试在web.config中的Application_Error事件中捕获错误 - 这至少允许您处理错误,即使您尝试显示的页面无法加载。






编辑添加



如果您正在运行IIS 7在集成模式下,如果您将错误页面的响应代码设置为500,则需要再做一件事:

  Response.TrySkipIisCustomErrors = true; 

但是,请注意,以下条件将阻止任何自定义的500错误页面显示,从而导致YSOD :


  1. 在web.config中的web.config中的某些处理程序发生错误,在web.config的其余部分之前触发(即大部分原始答案)

  2. 错误页面中的错误。


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 details 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.

这篇关于当global.asax或web.config中发生异常时,ASP.NET CustomErrors不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆