14

I have an action filter with the following signature

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class UnitOfWorkAttribute : ActionFilterAttribute

According to MSDN:

The AllowMultiple property indicates whether multiple instances of your attribute can exist on an element. If set to true, multiple instances are allowed; if set to false (the default), only one instance is allowed.

In MVC the behaviour seems a bit strange. When I decorated an action with this attribute, I found that the OnActionExecuting method of the filter was executed twice.

The filter was only declared on the action, not on the controller, and I had cleared any global filters. Could someone explain this behaviour?

Ben Foster
  • 34,340
  • 40
  • 176
  • 285
  • Is just _AllowMultiple_ that will cause _OnActionExecuting_ to be called twice, or did you indeed decorate your action with two instances of the attribute? – Codo Jul 18 '11 at 12:07
  • @Codo, no I don't need multiple instances so setting AllowMultiple to false resolves the problem, but it doesn't explain it. Possibly a bug in asp.net mvc? – Ben Foster Jul 18 '11 at 12:11
  • the action does really not fired twice ? only the attribute ? – dknaack Jul 18 '11 at 12:19
  • Is it possible that two actions really did fire? Transparent redirect, for example? – GalacticCowboy Jul 18 '11 at 12:29
  • The action method is executed once. The *OnActionExecuting* method of the ActionFilter fires twice. – Ben Foster Jul 18 '11 at 17:36
  • I would be interested in replicating this behaviour; can you make a sample project available for download? – Matt Griffiths Jul 22 '11 at 10:56
  • 1
    Do you have a custom base controller class with a filter on that method or any other unusual cases? Can you replicate with a brand new MVC 3 project? – Haacked Jul 28 '11 at 05:45
  • I created an attribute with AllowMultiple = true and it doesn't display this behavior. It only got called once. You can see the attribute here: http://pastebin.com/YtVX1YX5. – devlife Aug 12 '11 at 21:29

2 Answers2

32

I encountered the same problem. (I installed a global filter (just once) and discovered that its IActionFilter and IResultFilter methods were being called twice for each request. The filterContext.HttpContext object being passed to these methods was exactly the same for both calls.)

This turned out to be due to using Html.Action in the view. It appears (from looking at the call stack) that calling Html.Action reentrantly processes the child action method (during the processing of the initial action method) and the filters get invoked for both.

You can detect this situation by checking the filterContext.IsChildAction property.

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
0

I've had the OnActionExecuting method from my custom action filter class executing twice too.

At some point I added this to my Application_Start method in the global.asax.cs file:

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

Apparently, my action filter was already added to the Filters collection which was leading to the double call to OnActionExecuting and OnActionExecuted. So that line in the applicaton_start was not needed.

LordHits
  • 5,054
  • 3
  • 38
  • 51