Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you register a Web API ActionFilter without doing it globally?

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);
like image 971
akousmata Avatar asked Mar 22 '23 13:03

akousmata


1 Answers

see HttpConfiguration.Filters Property - it clearly says

Gets the list of filters that apply to all requests served using this HttpConfiguration instance.

but you need ActionFilter - which is, by definition,

Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.

so basically what you need to do is to remove

GlobalConfiguration.Configuration.Filters.Add(new MyAttribute());

line from your Global.asax.cs file.

like image 152
avs099 Avatar answered Apr 26 '23 12:04

avs099