Reputation: 933
In my WebApi 2 application, I've an action filter attribute in my base controller, this attribute has a boolean property with a default value which could be set in the constructor:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class MyActionFilterAttribute : ActionFilterAttribute
{
public bool MyProperty {get; set;}
public MyActionFilterAttribute(bool myProperty = true)
{
MyProperty = myProperty;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
if(MyProperty)
{
//DO Something
}
}
}
I've also a CustomValidatorFilter
configured in webApi config:
config.Filters.Add(new CustomValidatorFilterAttribute());
In some actions of my controller, I want to override the behaviour of MyActionFilterAttribute by setting the value of MyProperty
to false
, I've added the OverrideActionFilters
to my action:
[OverrideActionFilters]
[MyActionFilterAttribute(myProperty: false)]
public IHttpActionResult MyCation()
{
//Some staff here
}
But now my custom validation not working any more because of use of OverrideActionFilters
, Is there any way to redefine the OverrideActionFilters, or just to list the filter to override.
Thank you for your help.
Upvotes: 0
Views: 2314
Reputation: 709
You can use FindEffectivePolicy<TMetadata>()
method - this will get you the value of the relevant attribute (ie. will look for a declaration in the action and if not found will get you the one declared on the controller) -
@see FilterContext.FindEffectivePolicy<TMetadata>() method
public class MyActionFilterAttribute : ActionFilterAttribute
{
public bool MyProperty {get; set;}
public MyActionFilterAttribute(bool myProperty = true)
{
MyProperty = myProperty;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var effective = = context.FindEffectivePolicy<MyActionFilterAttribute>();
if(effective.MyProperty)
{
//DO Something
}
}
}
Upvotes: 0
Reputation: 933
I've created a specific attribute DoMyPropertyAttribute
and then I removed the property from the MyActionFilterAttribute
.
In MyActionFilterAttribute
I check if the action has `DoMyPropertyAttribute, if so I do the specific work:
public class DoMyPropertyAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if(actionContext.ActionDescriptor.GetCustomAttributes<DoMyPropertyAttribute>().Any())
{
//DO Something
}
}
}
In general, if we want override an action filter, we have just to skip it and then create a specific action filter that match the desired behaviour. To skip an action filter we can do:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if(actionContext.ActionDescriptor.GetCustomAttributes<SkipMyActionFilterAttribute>().Any())
{
return;
}
//Do something
}
}
Upvotes: 1