1

在与 RegisterGlobalFilters 和 HandleErrorAttribute 一直苦苦挣扎之后,我决定回到最基本的框架。我使用模板在 VS10 中创建了一个新的 MVC 3 项目。在 About-action 中添加 throw DivideByZeroException 并启动开发服务器。预计不会看到黄屏。

但我做到了。

为什么这对我不起作用?

更新

archil 和 Adam Tuliper 的建议有点奏效。错误视图被调用。

然后我继续在 RegisterGlobalFilters 中添加它。

filters.Add(new HandleErrorAttribute { ExceptionType = typeof(DivideByZeroException), View = "DivideByZeroException", Order = 1 });
filters.Add(new HandleErrorAttribute { View = "AllOtherExceptions", Order = 2 });

调用了 AllOtherExceptions 视图。为什么不使用 DivideByZeroException 视图?

后续问题已在此处发布。

4

2 回答 2

6

HandleErrorAttribute 在满足以下条件时起作用

  • Web 配置中启用了 CustomErrors
  • 如果异常为 HttpException,则其错误代码为 500。

在您的情况下,满足第二个条件,请确保您已打开自定义错误

<system.web>
     <customErrors mode="On"></customErrors>
</system.web>
于 2012-01-19T15:09:56.673 回答
2

您是否在 web.config 中打开了自定义错误?如果没有打开它,它应该可以工作。

于 2012-01-19T15:08:18.843 回答