使用自定义页面处理 IIS6、IIS7、ASP.NET MVC3 中的 500 错误的权威指南
我正在尝试将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 IIS 7+,您只缺少定义要使用自定义处理程序处理的 httpErrors 的部分:(
标记是可选的,具体取决于 对于 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:
(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".
最好的方法是首先找出 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.