web api中多个过滤器的执行顺序 [英] Order of execution with multiple filters in web api

查看:20
本文介绍了web api中多个过滤器的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是最新的web api.

我确实用 3 个不同的过滤器属性注释了一些控制器.

I do annotate some controllers with 3 different filter attributes.

1 [Authorize]
2 [RessourceOwnerAttribute derived from AuthorizationFilterAttribute]
3 [InvalidModelStateAttribute derived from ActionFilterAttribute]

我无法确定过滤器是否按照从上到下声明的顺序运行.

I can not be sure that the filters run in the order they are declared from top to down.

如何在web api 2.1中定义执行顺序?

How do I define the order of execution in web api 2.1 ?

https://aspnetwebstack.codeplex.com/workitem/1065#

http://aspnet.uservoice.com/forums/147201-asp-net-web-api/suggestions/3346720-execution-order-of-mvc4-webapi-action-filters

我还需要自己解决这个问题吗??

Do I still have to fix that for myself ??

推荐答案

这里需要注意的一些事情:

Some things to note here:

  1. 过滤器按以下顺序执行操作:全局定义的过滤器 -> 特定于控制器的过滤器 -> 特定于操作的过滤器.
  2. 授权过滤器 -> 操作过滤器 -> 异常过滤器
  3. 现在你似乎提到的问题与具有 多个 相同类型的过滤器(例如:多个ActionFilterAttribute 装饰在一个控制器或动作.这是不能保证的情况顺序是基于反射的.).对于这种情况,有一种方法使用自定义实现在 Web API 中完成System.Web.Http.Filters.IFilterProvider.我已经尝试了以下并做了一些测试来验证它.它似乎工作正常.您可以尝试一下,看看它是否按预期工作.

  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.

// 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; }
}

这篇关于web api中多个过滤器的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆