ASP.Net MVC 3 - HandleError 属性中的顺序似乎向后工作?

发布于 2024-11-27 11:35:36 字数 1462 浏览 1 评论 0原文

问题:当我使用 HandleError 属性的 Order 属性时,首先应用最高订单号,如下所示反对最低的优先。

详细信息

我想设置我的控制器以这种方式处理异常:

  • 如果异常的类型是 CustomException,请使用“CustomError”视图。
  • 否则,使用默认的“错误”视图。

使用默认的 MVC 项目模板,我完成了以下操作:

  • 从全局过滤器中删除了 HandleErrorAttribute
  • web.config 中启用了自定义错误。

然后,我将这些属性添加到 HomeController 类中:

[HandleError(ExceptionType = typeof(Exception), View = "Error")]
[HandleError(ExceptionType = typeof(CustomException), View = "CustomError")]

这工作正常 - 如果抛出 CustomException,则会显示“CustomError”视图;如果抛出Exception,则会显示“Error”视图。

但是,我认为 HandleError 属性上的 Order 属性会让事情变得更加明确。来自 MSDN 页面< /a>:

整数值越大,过滤器的优先级越低 是。

由此,我本以为以下代码会导致在所有情况下都显示“Error”视图:

[HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Error")]
[HandleError(Order = 2, ExceptionType = typeof(CustomException), View = "CustomError")]

但事实并非如此;这会导致与未指定 Order 属性时相同的行为。如果我交换订单,this 会导致显示“错误”视图,即使抛出 CustomException 也是如此。

基本上,实际的顺序优先级似乎与文档完全相反。我做错了什么还是这是一个错误?

Problem: When I use the Order property of the HandleError attribute, the highest order number gets applied first, as opposed to the lowest first.

Details

I want to set up my controller to handle exceptions in this way:

  • if the type of the exception is CustomException, use the "CustomError" view.
  • otherwise, use the default "Error" view.

Using the default MVC project template, I've done the following:

  • Removed the HandleErrorAttribute from the global filters.
  • Turned Custom Errors on in the web.config.

Then I add these attributes to the HomeController class:

[HandleError(ExceptionType = typeof(Exception), View = "Error")]
[HandleError(ExceptionType = typeof(CustomException), View = "CustomError")]

And this works fine - if a CustomException is thrown, the "CustomError" view is shown; if an Exception is thrown, the "Error" view is shown.

However, I thought the Order property on the HandleError attribute would make things more explicit. From the MSDN page:

The greater the integer value is, the lower the priority of the filter
is.

From this, I would have thought that the following code would cause the "Error" view to be shown in all circumstances:

[HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Error")]
[HandleError(Order = 2, ExceptionType = typeof(CustomException), View = "CustomError")]

But this is not the case; this causes the same behaviour as when the Order property was not specified. If I swap the Orders around, this causes the "Error" view to be shown, even if a CustomException was thrown.

Basically, the actual order priority seems to be exactly the opposite of the documentation. Am I doing something wrong or is this a bug?

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

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

发布评论

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

评论(2

情仇皆在手 2024-12-04 11:35:36

感谢您发布这个问题!不管你信不信,微软的 三年后,关于此功能的 MSDN 文档仍然不正确。我已向 ASP.NET 团队提交了错误报告来修复此问题。

Thanks for posting this question! Believe it or not, Microsoft's MSDN documentation on this feature is still incorrect three years later. I've submitted a bug report to the ASP.NET team to get this fixed.

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