I have seen how to do this globally in numerous posts and have it working in my code. The problem is that it's firing on EVERY call which isn't what I want, I only want it to fire on the calls to the methods where I have decorated the method with the attribute:
public class MyController : ApiController
{
[MyAttribute]
public void MethodA()
{
// Do Work - should have called the attribute filter
}
public void MethodB()
{
// Do Work - should NOT have called the attribute filter
}
}
This seems really basic to me and that I'm missing something but the only way I can get the attribute to fire at all is by registering it in global.asax
using GlobalConfiguration.Configuration.Filters.Add(new MyAttribute());
which causes it to fire on requests to both MethodA
and MethodB
. Is there any way to register the attribute and only fire on the methods where it is tagged? I have tried using AttributeUsage
to no avail.
EDIT Added code for attribute per comment although had to remove the inner workings. It's firing on all requests...
[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
// Do work
}
}
Edit 11/25 In addition to the information below where I accepted the answer I would like to add that a previous developer had removed the default ActionDescriptorFilterProvider with the following code that needed to be commented out in order for the default behavior of the custom action filters to take effect:
var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders();
var defaultprovider = providers.First(i => i is ActionDescriptorFilterProvider);
// This line was causing the problem.
GlobalConfiguration.Configuration.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);