Filter execute order in asp.net web api
https://stackoverflow.com/questions/21628467/order-of-execution-with-multiple-filters-in-web-api
Some things to note here:
- Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters.
- Authorization Filters -> Action Filters -> Exception Filters
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 ofSystem.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.- 如果有多个global action filters,这些filters的执行顺序按照config.Filters.Add的先后顺序来触发
- // Start clean by replacing with filter provider for global configuration.
- // For these globally added filters we need not do any ordering as filters are
- // executed in the order they are added to the filter collection
- config.Services.Replace(typeof(IFilterProvider), new System.Web.Http.Filters.ConfigurationFilterProvider());
- // Custom action filter provider which does ordering
- config.Services.Add(typeof(IFilterProvider), new OrderedFilterProvider());
- public class OrderedFilterProvider : IFilterProvider
- {
- public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
- {
- // controller-specific
- IEnumerable<FilterInfo> controllerSpecificFilters = OrderFilters(actionDescriptor.ControllerDescriptor.GetFilters(), FilterScope.Controller);
- // action-specific
- IEnumerable<FilterInfo> actionSpecificFilters = OrderFilters(actionDescriptor.GetFilters(), FilterScope.Action);
- return controllerSpecificFilters.Concat(actionSpecificFilters);
- }
- private IEnumerable<FilterInfo> OrderFilters(IEnumerable<IFilter> filters, FilterScope scope)
- {
- return filters.OfType<IOrderedFilter>()
- .OrderBy(filter => filter.Order)
- .Select(instance => new FilterInfo(instance, scope));
- }
- }
- //NOTE: Here I am creating base attributes which you would need to inherit from.
- public interface IOrderedFilter : IFilter
- {
- int Order { get; set; }
- }
- public class ActionFilterWithOrderAttribute : ActionFilterAttribute, IOrderedFilter
- {
- public int Order { get; set; }
- }
- public class AuthorizationFilterWithOrderAttribute : AuthorizationFilterAttribute, IOrderedFilter
- {
- public int Order { get; set; }
- }
- public class ExceptionFilterWithOrderAttribute : ExceptionFilterAttribute, IOrderedFilter
- {
- public int Order { get; set; }
- }
Filter execute order in asp.net web api的更多相关文章
- [转]ASP.NET web API 2 OData enhancements
本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session chsakell分享了前端使用AngularJS,后端使用ASP.NE ...
- 【ASP.NET Web API教程】5.1 HTTP消息处理器
原文:[ASP.NET Web API教程]5.1 HTTP消息处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内容. 5.1 HTTP ...
- ASP.NET Web API 框架研究 Action方法介绍
在根据请求解析出匹配的Controller类型并创建实例后,要在该Controller类型中的众多Action方法中选择与请求匹配的那一个,并执行,然后返回响应. Action方法,其元数据,主要包括 ...
- OData查询ASP.NET Web API全攻略
本篇使用ASP.NET Web API来体验OData各种query. 首先是本篇即将用到的Model.使用的OData版本是4.0. public class Customer { public i ...
- 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 ...
- Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...
- [转]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 ...
- 适用于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通用)>,现在重新整理一 ...
随机推荐
- OLE工具套件分析OFFICE宏恶意样本
零.绪论:OLE工具套件的介绍 OLE工具套件是一款针对OFFICE文档开发的具有强大分析功能一组工具集.这里主要介绍基于Python2.7的OLEtools的安装和使用. (1)Python版本需求 ...
- chrom调试
2.Event Listeners 可以看到事件找到对应在标签点开里有useCapture, passive: once: handler等等右键handler 的"show functio ...
- 【BZOJ1040】[ZJOI2008]骑士 树形DP
[BZOJ1040][ZJOI2008]骑士 Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情 ...
- 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地址以上三个 ...
- Yii2 高级模板不使用Apache配置目录,将前后台入口移到根目录
刚刚入手Yii2高级模板不久,部署项目时,得部署2个应用,个人感觉很繁琐,就将前后台入口文件全部拿到项目根目录.但是一看,完了,出错了!找教程找不到,还是自己解决吧 为了以后好升级,不改变Yii2核心 ...
- codeforces#512 Div2
pre过了三题 终测又挂了一题 又掉分了 真的是 太菜了 A-In Search of an Easy Problem 水题 有一个1就是hard #include <bits/stdc++.h ...
- hihoCoder_1449_后缀自动机三·重复旋律6
#1449 : 后缀自动机三·重复旋律6 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- 对10进制16位长的主键的缩短处理 NULL
# 对问题表去除旧有主键,新建自增主键:ALTER TABLE `question`CHANGE COLUMN `id` `id16` bigint(20) NULL COMMENT 'id_to_d ...
- Linux下Rsync+Inotify-tools实现数据实时同步
Linux下Rsync+Inotify-tools实现数据实时同步 注意:下面的三个案例都是rsync 每次都是全量的同步(这就坑爹了),而且 file列表是循环形式触发rsync ,等于有10个文件 ...
- CAD和GIS绘制图形分析
开发CAD和GIS绘图系统有些区别,自己试着分析一下. ♠ 首先CAD图形绘制主要管理图形,因此会有一个抽象的Geometry对象,软件维护一个Geometry对象的集合.如果以图层来管理几何图形,则 ...