OnActionExecuted being called twice in Web API

keremsefa picture keremsefa · Nov 26, 2013 · Viewed 8.4k times · Source

I am trying to do some stuff after my controller is done with the action at OnActionExecuted. However the method is called twice.

My filter method

public class TestFilter: ActionFilterAttribute
{
  public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {

       //do stuff here


    }
}

and my controller

[TestFilter]
  public class BaseController : ApiController
{
 public LoginResponseDTO Login(LoginRequestDTO loginRequestDTO)
    {

 //do login stuff
    }

}

when i try this filter, the onActionExecuted Method gets called twice which causes my action in the method to be applied twice to the response. I have searched for a reason but cannot find a solution.

Any Ideas?

Answer

lowselfesteemsucks picture lowselfesteemsucks · Sep 26, 2015

The answer is from @Martijn comments above:

 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
 public class TestFilter: ActionFilterAttribute

All credits goes to him. (Note: I'll remove the post, if he decide to add the comment as answer)