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

发布于 2024-12-02 00:03:19 字数 790 浏览 1 评论 0原文

我正在尝试将 500 错误处理页面添加到我的 ASP.NET MVC3 项目中。

我希望显示我的自定义错误页面,无论本地访问还是远程访问。 我的网站运行在 IIS6、IIS7 和 IIS6 上IIS7.5 Express

我希望它在以下情况下显示:

  • 在 Application_BeginRequest 中引发异常
  • 在 Application_Error 中引发异常
  • 在网站项目的静态构造函数中引发异常
  • 在控制器中引发
  • 异常 在视图中引发
  • 异常 异常几乎扔到任何地方。

我无法做到这一点,事实上我根本无法显示任何自定义错误页面。

我的错误页面位于 ~/Views/Shared/Error.aspx 中,

Global.asax.cs 中的 Application_Error 方法仅记录抛出的异常。

我的 web.config 有这个:

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

我缺少什么?我需要做什么来处理这些情况?

I'm trying to add a 500 error handling page to my ASP.NET MVC3 project.

I want my custom error page displayed regardless of local or remote access.
My website is running on IIS6,IIS7 & IIS7.5 Express

I want it displayed when:

  • An exception is thrown in Application_BeginRequest
  • An exception is thrown in 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

My Application_Error method in Global.asax.cs just logs the thrown exception.

My web.config has this:

<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-12-09 00:03:19

对于 IIS 7+,您只缺少定义要使用自定义处理程序处理的 httpErrors 的部分:(

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

标记是可选的,具体取决于 对于 IIS 6 及更低版本

,您必须通过 IIS 管理器进行设置,方法是转至相应的“属性”页、“自定义错误”选项卡,然后将相应的 HTTPError 行编辑为“消息类型:”“URL”并“网址:” “~/Views/Shared/Error.aspx”。

For IIS 7+, you're only missing the part that defines which httpErrors to handle with custom handlers:

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

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

二智少女 2024-12-09 00:03:19

最好的方法是首先找出 BeginRequest 抛出异常的原因。这不应该发生。在 Application_Error 中,一种替代方法是使用 GetBaseException,然后使用异常信息重定向到自定义错误页面。

The best approach is to find out why your BeginRequest is throwing an exception in the first place. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文