5

I made an ActionFilterAttribute that has the OnActionExecuted method implemented. That means, it runs after the Action method. But, in certain condition, I want the OnActionExecuted to not be executed.

How do I, from the Action method, prevent the ActionFilter from being executed?

For now, I have made this:

On the Action method:

RouteData.Values.Add("CancelActionFilter", true);

And on the ActionFilter.OnActionExecuted():

if (filterContext.RouteData.Values["CancelActionFilter"] != null)
{
    return;
}

But I think that may exist a more elegant approach.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Marcos Lima
  • 761
  • 1
  • 10
  • 26
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 23 '13 at 14:47

1 Answers1

3

OnActionExecuted is called inside the InvokeActionMethodFilter method in the ControllerActionInvoker class.

Inside this method there's nothing to prevent the action of been executed. I think yours is a good solution.

Code of ControllerActionInvoker class

thitemple
  • 5,833
  • 4
  • 42
  • 67