5

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);
10
  • 1
    What is MyClass here?...yes, you can decorate filter attributes on actions of a controller and only those would get invoked as you are expecting, but i see that here MyClass isn't a controller...
    – Kiran
    Nov 21, 2013 at 22:09
  • you should show the source for your attribute as well
    – Sam Axe
    Nov 21, 2013 at 22:53
  • It is an ApiController, updated the code to reflect that.
    – akousmata
    Nov 21, 2013 at 22:53
  • @Dan-o added code for the action filter.
    – akousmata
    Nov 21, 2013 at 22:57
  • @Kiran Challa yes, that's the way it worked in MVC 3, this is MVC 4 with a WebAPI controller and I haven't been able to figure out how to get it to work quite the same way.
    – akousmata
    Nov 21, 2013 at 23:02

2 Answers 2

7

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.

1
  • Hmmm, well, this is close to providing me with an answer. I see that this is the case with how it works on my OOB sample project. I'll take a deeper look at the interactions of our filters with the registration handler and report back. If it gets me the solution, I will mark this as answer, thanks.
    – akousmata
    Nov 22, 2013 at 22:17
0

Using filter on the Action or Controller level you need to register it in the same ConfigureServices method but as a service:

services.AddScoped<ActionFilterExample>();
services.AddScoped<ControllerFilterExample>();

Finally, to use a filter registered on the Action or Controller level, you need to place it on top of the Controller or Action as a ServiceType:

namespace AspNetCore.Controllers
{
    [ServiceFilter(typeof(ControllerFilterExample))]
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        [HttpGet]
        [ServiceFilter(typeof(ActionFilterExample))]
        public IEnumerable<string> Get()
        {
            return new string[] { "example", "data" };
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.