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:

  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的先后顺序来触发
// 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; }
}
复制代码

 

 

 

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(472)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-02-27 如何注释ascx中的代码
2016-02-27 怎么查询数据库中第30到40条记录呢? 通过ID,查询当前第30-40条记录 注意,ID不是顺序的
2015-02-27 List<T> please check srcIndex
2015-02-27 Form.ShowDialog和Form.DialogResult
点击右上角即可分享
微信分享提示