How to redirect a ToAction from an ActionFilterAttribute?

What is the best way to do a redirect (preferably redirecting to an action) from an ActionFilterAttribute?

I want to be able to pass data to a controller action from an ActionFilterAttribute.

+5
source share
1 answer

To redirect, override, OnActionExecutingand assign a new RedirectToRouteResultto filterContext.Result:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Result = new RedirectToRouteResult( 
            new RouteValueDictionary { { "action", "newActionName" },
                                       { "actionArgument", someData } });
    }

To assign data when redirecting, put it in the route as shown above.

+11
source

All Articles