加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – HandleErrorAttribute无法正常工作

发布时间:2020-12-16 00:06:24 所属栏目:asp.Net 来源:网络整理
导读:我在VS10中启动了一个MVC 3模板项目,并修改了global.asax.cs: public static void RegisterGlobalFilters(GlobalFilterCollection filters){ filters.Add(new HandleErrorAttribute { ExceptionType = typeof(DivideByZeroException),View = "DivideByZeroE
我在VS10中启动了一个MVC 3模板项目,并修改了global.asax.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute { ExceptionType = typeof(DivideByZeroException),View = "DivideByZeroException",Order = 1 });
    filters.Add(new HandleErrorAttribute { View = "AllOtherExceptions",Order = 2 });
}

到web.config我添加了:

<customErrors mode="On">

然后创建了相应的视图,最后在其中一个动作中添加了DivideByZero-throw.

结果:呈现了视图AllOtherExceptions.

解决方法

虽然我讨厌不同意达林所说的任何事情,但他错了.

设置属性没有问题(这就是你应该这样做的方式).

原始代码无法按预期工作的唯一原因是因为您设置了错误的订单.

见MSDN:

The OnActionExecuting(ActionExecutingContext),
OnResultExecuting(ResultExecutingContext),and
OnAuthorization(AuthorizationContext) filters run in forward order.
The OnActionExecuted(ActionExecutedContext),and
OnException(ExceptionContext) filters run in reverse order.

因此,您的通用AllOtherExceptions过滤器需要是最低订单号,而不是最高订单号.

希望下次有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读