14

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?

keremsefa
  • 141
  • 1
  • 3
  • Can you provide the relevant stack trace that shows both TestFilters being fired in one callstack? – Haney Nov 26 '13 at 14:25
  • I can see the same thing happening in my code. A breakpoint in the controller and the onactionexectued shows that the controller is fired first, then OnActionExectued is fired twice. – Andiih Feb 10 '14 at 13:00
  • 22
    I had the same issue [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)] fixed it for me. – Martijn Apr 12 '14 at 12:35
  • @Martijn that worked perfectly... Wish you had said that as an answer so I could up vote it... – JustMaier Jul 26 '14 at 15:35

4 Answers4

24

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)

lowselfesteemsucks
  • 747
  • 1
  • 10
  • 21
  • 1
    In my case I need to add only [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] as rest everything derived from ActionFilterAttribute class. – Aamol Sep 13 '17 at 07:16
1

For me the issue was I was calling /myApi/action which was redirecting to /myApi/action/ and this caused OnActionExecuted() to run twice.

I filtered out where filterContext.Result is RedirectResult within OnActionExecuted since I wasn't interested in running my code then. The HTTP status code showed as 200 on both the calls so filtering by that won't work.

C.M.
  • 1,144
  • 9
  • 14
0

You can override the AllowMultiple inside your ActionFilterAttribute, like so:

public override bool AllowMultiple { get { return false; } }

public override void OnActionExecuting(HttpActionContext actionContext)
{
  //Your logic
}

That will stop your ActionFilter being called twice. Also check that it is not registered twice. Check out this stackoverflow answer to see more about that.

Do be aware that AttributeUsage attribute is a single-use attribute--it can't be applied more than once to the same class, as you will find in the remarks section of this.

0

If you have registered the custom filter in Global.asax.cs, like this:

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

Please revoke the attribute above your custom controller.

auxo
  • 13
  • 2