执行顺序与网页API多个过滤器 [英] Order of execution with multiple filters in web api

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

问题描述

我使用最新的网​​页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.

如何定义在执行顺序网​​页API 2.1

<一个href=\"https://aspnetwebstack.$c$cplex.com/workitem/1065#\">https://aspnetwebstack.$c$cplex.com/workitem/1065#

<一个href=\"http://aspnet.uservoice.com/forums/147201-asp-net-web-api/suggestions/3346720-execution-order-of-mvc4-webapi-action-filters\">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; }
}


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

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