0
votes

On OnActionExecuting for a controller, I'm trying to do a permanent redirect. The code looks like this:

protected override void OnActionExecuting(ActionExecutingContext filterContext) {
  var resolvedUrl = "http://www.cnn.com";
  filterContext.Result = new RedirectResult(resolvedUrl);
  filterContext.Result.ExecuteResult(filterContext);
  return;
}

It's not immediately redirecting because this method is in a base class, and after the return statement, the inherited class's OnActionExecuting method is called.

Question: how do I do the redirect and stop there, and not do anything in the inherited class. Thanks.

Note: in the child class (one that's inheriting from base), I have base.OnActionExecuting(filterContext); in its OnActionExecuting.

1

1 Answers

0
votes

You shouldn't call ExecuteResult() from within an action filter. Setting filterContext.Result to a non-null value is sufficient to prevent the MVC pipeline from invoking further filters.

The subclassed type should instead be changed to read:

base.OnActionExecuting(filterContext);
if (filterContext.Result != null) {
  return; // something got short-circuited
}