Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToAction From ActionFilter

Tags:

asp.net-mvc

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);
    }   
}
like image 427
Yoann. B Avatar asked Jul 28 '09 08:07

Yoann. B


2 Answers

Try this:

filterContext.Result = new RedirectToRouteResult(
    new System.Web.Routing.RouteValueDictionary {
        {"controller", "Error"}, {"action", "ServiceNotFound"}
    }
);
like image 116
eu-ge-ne Avatar answered Sep 23 '22 06:09

eu-ge-ne


You don't really. You can either use a RedirectResult or RedirectToRouteResult. If you are looking at redirecting away based on authentication, you should consider that a Controller is an ActionFilter, so you can probably inherit this basic behaviour from a base controller class. Just override the OnActionExecuting method in base class.

like image 45
Mark Dickinson Avatar answered Sep 22 '22 06:09

Mark Dickinson