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

asp.net-core – Asp.Net核心Web应用程序:使用IExceptionFilter

发布时间:2020-12-16 07:30:23 所属栏目:asp.Net 来源:网络整理
导读:Asp.Net Core支持两种方法为Web应用程序执行全局异常处理,实现IExceptionFilter或创建自定义中间件.一个优于另一个有什么优势吗?我看到的大多数引用都是用于创建自定义中间件. 解决方法 ASP.NET Core docs解释了这两种方法之间的主要区别.在具体讨论“异常
Asp.Net Core支持两种方法为Web应用程序执行全局异常处理,实现IExceptionFilter或创建自定义中间件.一个优于另一个有什么优势吗?我看到的大多数引用都是用于创建自定义中间件.

解决方法

ASP.NET Core docs解释了这两种方法之间的主要区别.在具体讨论“异常过滤器”时,文档说明:

Exception filters handle unhandled exceptions that occur in controller creation,model binding,action filters,or action methods. They won’t catch exceptions that occur in Resource filters,Result filters,or MVC Result execution.

甚至有关何时使用中间件以及何时使用“异常过滤器”的建议:

Exception filters are good for trapping exceptions that occur within MVC actions,but they’re not as flexible as error handling middleware. Prefer middleware for the general case,and use filters only where you need to do error handling differently based on which MVC action was chosen. For example,your app might have action methods for both API endpoints and for views/HTML. The API endpoints could return error information as JSON,while the view-based actions could return an error page as HTML.

(编辑:李大同)

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

    推荐文章
      热点阅读