asp.net-mvc – ExceptionContext.ExceptionHandled更改为true.
我正在使用全局操作过滤器来处理和记录所有异常.
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ElmahHandleErrorAttribute()); filters.Add(new HandleErrorAttribute()); } 这是全局操作过滤器ElmahHandleErrorAttribute的定义方式 – 它会覆盖OnException方法. public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute { public override void OnException(ExceptionContext context) { //Is the exception handled already? context.ExceptionHandled seems to be true here if (!context.IsChildAction && (context.HttpContext.IsCustomErrorEnabled)) { //Do other stuff stuff //Log to Elmah } } ... } 我不明白为什么当OnException方法执行时context.ExceptionHandled的值为true. -编辑- <customErrors mode ="On" defaultRedirect="Error/General"> <error statusCode="404" redirect="Error/Http404"/> </customErrors> 我不明白的是,控制器操作General不会被执行(断点永远不会被命中),但是当ElmahHandleErrorAttribute的OnException方法开始执行时,ExceptionContext.ExceptionHandled的值被设置为true. 解决方法
发生异常时,全局过滤器的顺序执行
in reverse order.这意味着HandleErrorAttribute首先运行.
您可以查看HandleErrorAttribute here的代码,但简而言之,它: >仅在ExceptionHandled为false且仅启用自定义错误时执行. 因为它是第一个过滤器,所以ExceptionHandled在执行时为false,导致它将视图设置为Error并将ExceptionHandled设置为true.那么,当你自己的过滤器执行时,这就是ExceptionHandled已经设置为true的原因.请注意,如果禁用了自定义错误,那么ExceptionHandled仍然是false,因为HandleErrorAttribute不会完成它的工作.在这种情况下,ELMAH无论如何都会记录错误,因为它是未处理的(黄色死亡屏幕),因此您班级中的测试是为了防止重复记录错误. 现在,关于乳清未执行常规操作的另一个问题,仅当过滤器本身未设置某些显式重定向时才使用defaultRedirect,因此当ActionMethod中发生异常并且您具有全局时,它实际上被忽略过滤器HandleErrorAttribute已注册.但是,如果您输入的URL不存在,则会调用它,即在ActionMethod中不会发生错误.此外,如果您注释掉行以在Global.asax.cs中注册HandleErrorAttribute,那么您将始终执行General控制器操作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 使用breeze js不直接与DBContext交互
- .net – 制作类似TinyURL.com的简短网址
- asp.net – 用户角色 – 为什么不存储在会话中?
- X-Admin&ABP框架开发-设置管理
- asp.net-ajax – 建议使用不引人注意的AJAX与MVC 5?
- asp.net-mvc – ASP.NET MVC 4 Datagrid
- asp.net – viewstate到期了吗?
- asp.net-mvc – 无法加载汇编WebPages.Deployment版本2在主
- 优化 – 在经典ASP内存泄漏中实现对象缓存
- 使用VSCode如何调试C#控制台程序_2_加深总结