How to get filter to redirect to another action?
Solution 1
RedirectToAction
is just a helper method to construct a RedirectToRouteResult()
, so what you do is simply create a new RedirectToRouteResult()
passing along a RouteValueDictionary()
with values for your action.
Complete sample based on code from @Domenic in the comment below:
public class IsGuestAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Home" },
{ "action", "Index" }
});
}
}
}
Solution 2
Here's a code example:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Ctx.User.IsGuest)
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "Index");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
Solution 3
I know that I'm a little bit late to the party, but I used veggerby's solution and built a helper class that may be useful to some people, so I wanted to provide it here:
public static class ActionFilterHelpers
{
public static void Redirect(this ActionExecutingContext filterContext, String controller, String action, object routeValues)
{
filterContext.Result = Redirect(controller, action, routeValues);
}
public static ActionResult Redirect(String controller, String action, object routeValues)
{
var routeValues = new RouteValueDictionary();
routeValues["controller"] = controller;
routeValues["action"] = action;
foreach (var keyValue in new ObjectDictionary(routeValues))
routeValues.Add(keyValue.Key, keyValue.Value);
return new RedirectToRouteResult(routeValues);
}
}
I provided both a static method that returns a redirect ActionResult
and an extension method that extends filterContext
. Hope someone finds this useful.
ObjectDictionary
is a class that uses reflection to create a dictionary from the properties of the object from which it is constructed. I didn't include that code because I believe there is a better way to do that somewhere in the framework. I haven't found it yet, but I don't want others to inherit my potential bugs.
Related videos on Youtube
Igor Golodnitsky
Hey, I introduce a service for building web applications. Write this here just for better PageRank) excel.appqui.com
Updated on July 05, 2022Comments
-
Igor Golodnitsky almost 2 years
RedirectToAction
is protected, and we can use it only inside actions. But if I want to redirect in a filter?public class IsGuestAttribute: ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!Ctx.User.IsGuest) filterContext.Result = (filterContext.Controller as Controller) .RedirectToAction("Index", "Home"); } }
-
Domenic over 13 yearsOr just
new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Home" }, {"action", "HomePage" } })
-
CRice about 13 yearsIf only you could have said that comment with a green resharper underline
-
demoncodemonkey almost 9 yearsYou can also put this code in the
OnException
method, as long as you setfilterContext.ExceptionHandled = true;
-
demoncodemonkey almost 9 yearsYou can also put this code in the
OnException
method, as long as you setfilterContext.ExceptionHandled = true;