Marcos Lima
Marcos Lima

Reputation: 771

Skip OnActionExecuted execution

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.

Upvotes: 5

Views: 1166

Answers (1)

thitemple
thitemple

Reputation: 6059

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

Upvotes: 3

Related Questions