https://stackoverflow.com/questions/21628467/order-of-execution-with-multiple-filters-in-web-api

Some things to note here:

  1. Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters.
  2. Authorization Filters -> Action Filters -> Exception Filters
  3. Now the problem that you seem to mention is related to having multiple filters of the same kind (ex: Multiple ActionFilterAttribute decorated on a controller or an action. This is the case which would not guarantee the order as its based on reflection.). For this case, there is a way to do it in Web API using custom implementation of System.Web.Http.Filters.IFilterProvider. I have tried the following and did some testing to verify it. It seems to work fine. You can give it a try and see if it works as you expected.

  4. 如果有多个global action filters,这些filters的执行顺序按照config.Filters.Add的先后顺序来触发
  1. // Start clean by replacing with filter provider for global configuration.
  2. // For these globally added filters we need not do any ordering as filters are
  3. // executed in the order they are added to the filter collection
  4. config.Services.Replace(typeof(IFilterProvider), new System.Web.Http.Filters.ConfigurationFilterProvider());
  5.  
  6. // Custom action filter provider which does ordering
  7. config.Services.Add(typeof(IFilterProvider), new OrderedFilterProvider());
  1. public class OrderedFilterProvider : IFilterProvider
  2. {
  3. public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
  4. {
  5. // controller-specific
  6. IEnumerable<FilterInfo> controllerSpecificFilters = OrderFilters(actionDescriptor.ControllerDescriptor.GetFilters(), FilterScope.Controller);
  7.  
  8. // action-specific
  9. IEnumerable<FilterInfo> actionSpecificFilters = OrderFilters(actionDescriptor.GetFilters(), FilterScope.Action);
  10.  
  11. return controllerSpecificFilters.Concat(actionSpecificFilters);
  12. }
  13.  
  14. private IEnumerable<FilterInfo> OrderFilters(IEnumerable<IFilter> filters, FilterScope scope)
  15. {
  16. return filters.OfType<IOrderedFilter>()
  17. .OrderBy(filter => filter.Order)
  18. .Select(instance => new FilterInfo(instance, scope));
  19. }
  20. }
  1. //NOTE: Here I am creating base attributes which you would need to inherit from.
  2. public interface IOrderedFilter : IFilter
  3. {
  4. int Order { get; set; }
  5. }
  6.  
  7. public class ActionFilterWithOrderAttribute : ActionFilterAttribute, IOrderedFilter
  8. {
  9. public int Order { get; set; }
  10. }
  11.  
  12. public class AuthorizationFilterWithOrderAttribute : AuthorizationFilterAttribute, IOrderedFilter
  13. {
  14. public int Order { get; set; }
  15. }
  16.  
  17. public class ExceptionFilterWithOrderAttribute : ExceptionFilterAttribute, IOrderedFilter
  18. {
  19. public int Order { get; set; }
  20. }

Filter execute order in asp.net web api的更多相关文章

  1. [转]ASP.NET web API 2 OData enhancements

    本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...

  2. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session chsakell分享了前端使用AngularJS,后端使用ASP.NE ...

  3. 【ASP.NET Web API教程】5.1 HTTP消息处理器

    原文:[ASP.NET Web API教程]5.1 HTTP消息处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内容. 5.1 HTTP ...

  4. ASP.NET Web API 框架研究 Action方法介绍

    在根据请求解析出匹配的Controller类型并创建实例后,要在该Controller类型中的众多Action方法中选择与请求匹配的那一个,并执行,然后返回响应. Action方法,其元数据,主要包括 ...

  5. OData查询ASP.NET Web API全攻略

    本篇使用ASP.NET Web API来体验OData各种query. 首先是本篇即将用到的Model.使用的OData版本是4.0. public class Customer { public i ...

  6. ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 Part 5 (by TAISEER)

    https://www.cnblogs.com/KimmyLee/p/6430474.html https://www.cnblogs.com/rocketRobin/p/9077523.html h ...

  7. Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...

  8. [转]Supporting OData Query Options in ASP.NET Web API 2

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/suppor ...

  9. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

随机推荐

  1. OLE工具套件分析OFFICE宏恶意样本

    零.绪论:OLE工具套件的介绍 OLE工具套件是一款针对OFFICE文档开发的具有强大分析功能一组工具集.这里主要介绍基于Python2.7的OLEtools的安装和使用. (1)Python版本需求 ...

  2. chrom调试

    2.Event Listeners 可以看到事件找到对应在标签点开里有useCapture, passive: once: handler等等右键handler 的"show functio ...

  3. 【BZOJ1040】[ZJOI2008]骑士 树形DP

    [BZOJ1040][ZJOI2008]骑士 Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情 ...

  4. linux :故障提示:Error:No suitable device found: no device found for connection "System eth0"

    解决:1./etc/udev/rules.d/70-persistent-net.rules 中的mac地址2.网卡配置文件ifcfg-eth0中的mac地址3.ifconfig中的mac地址以上三个 ...

  5. Yii2 高级模板不使用Apache配置目录,将前后台入口移到根目录

    刚刚入手Yii2高级模板不久,部署项目时,得部署2个应用,个人感觉很繁琐,就将前后台入口文件全部拿到项目根目录.但是一看,完了,出错了!找教程找不到,还是自己解决吧 为了以后好升级,不改变Yii2核心 ...

  6. codeforces#512 Div2

    pre过了三题 终测又挂了一题 又掉分了 真的是 太菜了 A-In Search of an Easy Problem 水题 有一个1就是hard #include <bits/stdc++.h ...

  7. hihoCoder_1449_后缀自动机三·重复旋律6

    #1449 : 后缀自动机三·重复旋律6 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...

  8. 对10进制16位长的主键的缩短处理 NULL

    # 对问题表去除旧有主键,新建自增主键:ALTER TABLE `question`CHANGE COLUMN `id` `id16` bigint(20) NULL COMMENT 'id_to_d ...

  9. Linux下Rsync+Inotify-tools实现数据实时同步

    Linux下Rsync+Inotify-tools实现数据实时同步 注意:下面的三个案例都是rsync 每次都是全量的同步(这就坑爹了),而且 file列表是循环形式触发rsync ,等于有10个文件 ...

  10. CAD和GIS绘制图形分析

    开发CAD和GIS绘图系统有些区别,自己试着分析一下. ♠ 首先CAD图形绘制主要管理图形,因此会有一个抽象的Geometry对象,软件维护一个Geometry对象的集合.如果以图层来管理几何图形,则 ...