OnActionExecuted being called twice in Web API
Asked Answered
M

5

15

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?

Mannikin answered 26/11, 2013 at 14:22 Comment(4)
Can you provide the relevant stack trace that shows both TestFilters being fired in one callstack?Tarratarradiddle
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.Caesalpiniaceous
I had the same issue [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)] fixed it for me.Judkins
@Judkins that worked perfectly... Wish you had said that as an answer so I could up vote it...Spires
E
27

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)

Eanore answered 26/9, 2015 at 12:35 Comment(1)
In my case I need to add only [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] as rest everything derived from ActionFilterAttribute class.Overflow
S
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.

Swink answered 18/8, 2017 at 22:11 Comment(0)
S
1

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.

Sidwel answered 25/7, 2019 at 12:46 Comment(0)
K
1

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.

Kanazawa answered 11/1, 2021 at 0:45 Comment(1)
This was my issue, except in the WebApiConfig.cs fileDeepfry
L
0

Make sure [BasicAuthenticateFilter] is not in "Controller" level as well as "Method" level. It will run twice.

Laney answered 18/10, 2022 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.