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??
ASP.NET MVC 5 has arrived with a very important feature called Filter Overrides. Using the Filter Overrides feature, we can exclude a specific action method or controller from the global filter or controller level filter. ASP.NET MVC 5 has arrived with a very important feature called Filter Overrides.
In MVC applications, we can override the filters which are applied at the Global or the Controller level. For example, we have the "Authorize" filter applied at the Controller, which restricts the user types who can access the Controller Methods. This filter will apply on all the methods of the Controller.
ASP.NET MVC 5 has a new feature called Filter Overrides, which allows you to clear or replace certain filter types created in higher scopes. For example, if you created a global action filter or controller action filter, you could override those filters on a case-by-case basis at the controller action level.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With