Mori
Mori

Reputation: 2574

Using IOverrideFilter to override custom ActionFilters

I want to use IOverrideFilter interface to override my custom global filter but it is simply not working! Code looks like to be as follow:

public sealed class MyGlobalFilterExceptionAttribute : FilterAttribute, IOverrideFilter
{
    public Type FiltersToOverride
    {
        get { return typeof(ITest); }
    }
}

My global filter has implemented ITest interface. I know I can implement the task in my original global filter but I would like to do so by IOverrideFilter.

Any Idea??

Upvotes: 7

Views: 1559

Answers (1)

Daniel J.G.
Daniel J.G.

Reputation: 34992

The msdn info is not entirely clear about it but IOverrideFilter.FiltersToOverride must be exactly one of the following:

  • IActionFilter
  • IAuthorizationFilter
  • IAuthenticationFilter
  • IExceptionFilter

Basically, you cannot override specific filters, you can only override all the filters of one of the categories above. Have a look a the ProcessOverrideFilters method in the source code.

So, let's say that your ITest filter is of type IActionFilter, then your override will be (The same logic would apply for any other filter category):

public Type FiltersToOverride
{
    get { return typeof(IActionFilter); }
}

You could also use the predefined OverrideActionFilters (and similar predefined override attributes for other filter categories).

For a more fine-grained override, you might need to develop specific solutions like this one or write your own filter provider as in this very nice article

Upvotes: 8

Related Questions