почему следующий вывод?

Некоторые примечания:

  1. Фильтры выполняются в следующем порядке для действия: глобально определяемые фильтры -> Фильтры, специфичные для контроллера, -> Фильтры, зависящие от конкретных действий.
  2. Фильтры авторизации -> Фильтры действий -> Фильтры исключения
  3. Теперь проблема, о которой вы говорите, связана с наличием multiple фильтров одного и того же типа ( ex: несколько ActionFilterAttribute, декорированных на контроллере или действии. Это тот случай, который не гарантировал бы порядок, основанный на отражении.). Для этого случая есть способ сделать это в веб-API, используя пользовательскую реализацию System.Web.Http.Filters.IFilterProvider. Я попробовал следующее и сделал некоторые проверки, чтобы проверить это. Кажется, он работает нормально. Вы можете попробовать и посмотреть, работает ли он так, как вы ожидали.
    // 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; }
    }
    
-8
задан Vineet1982 12 April 2013 в 08:01
поделиться