سؤال

I have two filters added to a WebAPI controller:

[FooFilter]
[BarFilter]
public IQueryable<SomeEntity> GetSomething()
{
    ...

Unfortunately, they are being executed in random order.

For the MVC stack, there's the Order property where one could just do

[FooFilter(Order = 1)]
[BarFilter(Order = 2)]
public IQueryable<SomeEntity> GetSomething()
{
    ...

There's an article on StrathWeb about adding Order support to WebAPI, which seems outdated and just won't compile for me.

Do the C# wizards of SO have any advice on how to implement/stub the Order attribute for WebAPI?

هل كانت مفيدة؟

المحلول

The solution from the article works fine and compiles.

Just make sure you use the correct namespaces, since some of the classes have the same name under System.Web.Mvc and System.Web.Http namespaces.

These are the fully qualified names you should be referencing:

System.Web.Http.Filters.IFilter
System.Web.Http.Filters.ActionFilterAttribute
System.Web.Http.Filters.FilterScope
System.Web.Http.Filters.FilterInfo
System.Web.Http.Filters.IFilterProvider

Also, remember that once implemented, your filters need to start inheriting from BaseActionFilterAttribute instead of the original ActionFilterAttribute (that's the only way you'd get the Position property).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top