RedirectToAction from ActionFilter

How to access RedirectToAction from a custom ActionFilter?

public class ExceptionHandlingFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Exception != null && !filterContext.ExceptionHandled)
        {
            filterContext.ExceptionHandled = true;

            // HERE : RedirectToAction("ServiceNotFound","Error");

        }
        base.OnActionExecuted(filterContext);
    }   
}
+5
source share
2 answers

Try the following:

filterContext.Result = new RedirectToRouteResult(
    new System.Web.Routing.RouteValueDictionary {
        {"controller", "Error"}, {"action", "ServiceNotFound"}
    }
);
+8
source

In fact. You can use RedirectResult or RedirectToRouteResult. If you are looking for authentication-based redirection, you should consider that the controller is an ActionFilter, so you can probably inherit this basic behavior from the controller's base class. Just override the OnActionExecuting method in the base class.

+1
source

All Articles