Using IOverrideFilter to Override Custom ActionFilters

I want to use the IOverrideFilter interface to override my custom global filter, but it just doesn't work! The code is as follows:

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

My global filter implemented the ITest interface. I know that I can implement the task in my original global filter, but I would like to do it with IOverrideFilter .

Any idea ??

+6
source share
1 answer

The msdn information is not entirely clear, but IOverrideFilter.FiltersToOverride should be exactly one of the following

  • IActionFilter
  • IAuthorizationFilter
  • IAuthenticationFilter
  • IExceptionFilter

Basically, you cannot redefine certain filters, you can only redefine all filters of one of the above categories. See the ProcessOverrideFilters method in the source code .

So, let's say that your ITest filter is of type IActionFilter , then your redefinition will be (the same logic applies to any other category of filters):

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

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

For a smaller-scale redefinition, you may need to develop specific solutions like this one or write your own filter provider as in this very good article

+7
source

Source: https://habr.com/ru/post/988729/


All Articles