简体   繁体   English

使用自定义页面处理IIS6,IIS7,ASP.NET MVC3中的500个错误的权威指南

[英]Definitive Guide to Handling 500 Errors in IIS6, IIS7, ASP.NET MVC3 with Custom Page

I'm trying to add a 500 error handling page to my ASP.NET MVC3 project. 我正在尝试将500错误处理页面添加到我的ASP.NET MVC3项目中。

I want my custom error page displayed regardless of local or remote access. 我希望无论本地访问还是远程访问都显示我的自定义错误页面。 My website is running on IIS6,IIS7 & IIS7.5 Express 我的网站在IIS6,IIS7和IIS7.5 Express上运行

I want it displayed when: 我希望在以下情况下显示它:

  • An exception is thrown in Application_BeginRequest 在Application_BeginRequest中引发异常
  • An exception is thrown in Application_Error 在Application_Error中引发异常
  • An exception is thrown in a static constructor in the Website Project 网站项目中的静态构造函数中引发了异常
  • An exception is thrown in a Controller 在控制器中引发异常
  • An exception is thrown in a view 视图中抛出异常
  • An exception thrown anywhere pretty much. 几乎在任何地方都会引发异常。

I haven't been able to do in this, in fact I haven't been able to get any custom error pages to display at all. 我无法做到这一点,实际上我根本无法显示任何自定义错误页面。

My error page lives in ~/Views/Shared/Error.aspx 我的错误页面位于~/Views/Shared/Error.aspx

My Application_Error method in Global.asax.cs just logs the thrown exception. 我在Global.asax.cs中的Application_Error方法仅记录引发的异常。

My web.config has this: 我的web.config具有:

<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.aspx" redirectMode="ResponseRewrite">
</customErrors>
...
<system.webServer>
  <httpErrors errorMode="Custom" />
  ...
</system.webServer>

What am I missing? 我想念什么? What do I need to do to handle these scenarios? 我需要怎么做才能处理这些情况?

For IIS 7+, you're only missing the part that defines which httpErrors to handle with custom handlers: 对于IIS 7+,你唯一缺少的用来定义httpErrors与自定义处理程序来处理的部分:

<configuration>
   <system.webServer>
      <httpErrors errorMode="Custom">
         <remove statusCode="500" />
         <error statusCode="500" path="~/Views/Shared/Error.aspx" />
       </httpErrors>
   </system.webServer>
</configuration>

(The <remove /> tag is optional, depending on your web.config hierarchy.) <remove />标记是可选的,具体取决于您的web.config层次结构。)

For IIS 6 and below, You have to set this via the IIS Manager by going to the appropriate Properties page, Custom Errors tab, then edit the appropriate HTTPError line to "Message type:" "URL" and "URL:" "~/Views/Shared/Error.aspx". 对于IIS 6及以下版本,您必须通过IIS管理器进行设置,方法是转到相应的“属性”页面的“自定义错误”选项卡,然后将相应的HTTPError行编辑为“消息类型:”“ URL”和“ URL:”“〜/查看/共享/ Error.aspx”。

The best approach is to find out why your BeginRequest is throwing an exception in the first place. 最好的方法是找出为什么BeginRequest首先会引发异常。 This should not be happening. 这不应该发生。 In Application_Error, one alternative is to use GetBaseException and then just redirect to your custom error page with the exception information. 在Application_Error中,一种替代方法是使用GetBaseException,然后仅将异常信息重定向到您的自定义错误页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM