23

Closed

WebApi: Filters can't have a set execution order

description

MVC filter attributes support the Order property for setting the execution order when multiple instances are applied to a controller or method, those in Web API don't.
Closed May 16, 2014 at 5:34 PM by yishaigalatzer

comments

yishaigalatzer wrote Jun 11, 2013 at 3:47 PM

As a work around you can look into overriding GetFilterPipeLine in HttpActionDescriptor.
public override Collection<FilterInfo> GetFilterPipeline()
        {
            Collection<FilterInfo> originalFilters = _innerDescriptor.GetFilterPipeline();
            Collection<FilterInfo> newFilters = new Collection<FilterInfo>();

            // AS AN EXAMPLE, HERE IS HOW TO REPLACE A FILTER WITH YOUR OWN
            // SIMILARLY YOU CAN CHANGE THE ORDER
            // for any actions that support query composition, we need to replace it with our
            // query filter.
            foreach (FilterInfo filterInfo in originalFilters)
            {
                FilterInfo newInfo = filterInfo;
                QueryableAttribute queryableFilter = filterInfo.Instance as QueryableAttribute;
                if (queryableFilter != null)
                {
                    newInfo = new FilterInfo(new QueryFilterAttribute() { ResultLimit = queryableFilter.ResultLimit }, filterInfo.Scope);
                }
                newFilters.Add(newInfo);
            }

            return newFilters;
        }

VladimirSachek wrote Apr 4, 2014 at 7:36 PM

Added pull request with Order property in IFilter interface. Order has bigger priority than attribute scope as it is in MVC case.
https://aspnetwebstack.codeplex.com/SourceControl/network/forks/VladimirSachek/WebApiFilterOrderSpike/contribution/6548

yishaigalatzer wrote May 16, 2014 at 5:34 PM

We are tracking all vNext items on https://github.com/aspnet/MVC/issues

This particular item is already completed and you can try it out using one of the dev builds on the ASP.NET vNext MyGet feed.

For instructions on how to get started: https://github.com/aspnet/home#getting-started