Can you register the ActionFilter web API without doing this globally?

I have seen how to do this globally in numerous posts and work in my code. The problem is that it launches an EVERY call, which is not what I want, I only want it to make calls to methods, where I decorated the method with an 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
    }
}

It seems to me really basic, and that I am missing something, but the only way I can get the attribute is to register it global.asaxwith the help GlobalConfiguration.Configuration.Filters.Add(new MyAttribute());that forces it to run queries on both MethodAand MethodB. Is there a way to register an attribute and only fire by the methods where it is marked? I tried to use to AttributeUsageno avail.

EDIT , . ...

[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        // Do work
    }
}

11/25 , , , ActionDescriptorFilterProvider , , :

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);
+4
1

. HttpConfiguration.Filters -

, , HttpConfiguration.

ActionFilter -

, . , , .

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

Global.asax.cs.

+7

All Articles