Ben Foster
Ben Foster

Reputation: 34800

ActionFilter not firing after setting result in controller.OnActionExecuting

I have a global action filter that is setting the MasterPage of all ViewResults during the OnActionExecuting event.

In a number of my controllers (where each controller represents a feature of the application) I need to check to see if the feature is enabled and if not, return a different View.

Here's the code:

    protected override void OnActionExecuting(ActionExecutingContext filterContext) {
        if (!settings.Enabled)
        {
            filterContext.Result = View("NotFound");
        }

        base.OnActionExecuting(filterContext);
    }

The problem is that when setting the result like this, my ActionFilter's OnActionExecuted method does not fire, meaning I do not get the correct MasterPage applied.

I would like to understand why this happens. One remedy is to move my ActionFilter logic into OnResultExecuting (this does fire), but I am still confused as to why OnActionExecuted does not.

Many thanks

Ben

Upvotes: 3

Views: 2857

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038910

If you assign a result to the filterContext.Result inside an OnActionExecuting then the action won't execute => the OnActionExecuted will never run. So you might need to apply the correct master page inside the OnActionExecuting event when returning the NotFound view:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (!settings.Enabled)
    {
        // Because we are assigning a Result here the action will be 
        // short-circuited and will never execute neither the OnActionExecuted
        // method of the filer. The NotFound view will be directly rendered
        filterContext.Result = new ViewResult
        {
            ViewName = "NotFound",
            MasterName = GetMasterName()
        };
    }
}

Upvotes: 6

Adam Tuliper
Adam Tuliper

Reputation: 30152

As an alternative how about assigning the master page (layout) in _viewstart.cshtml and not worry about the filter?

Upvotes: 0

Related Questions