如何获得filterredirect到另一个行动?
RedirectToAction
是受保护的,我们只能在动作中使用它。 但是,如果我想在一个filterredirect?
public class IsGuestAttribute: ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!Ctx.User.IsGuest) filterContext.Result = (filterContext.Controller as Controller) .RedirectToAction("Index", "Home"); } }
RedirectToAction
只是构造一个RedirectToRouteResult()
的帮助器方法,所以你要做的就是创build一个新的RedirectToRouteResult()
传递一个RouteValueDictionary()
值给你的动作。
根据以下注释中的@Domenic代码完成示例:
public class IsGuestAttribute: ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!Ctx.User.IsGuest) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" } }); } } }
这是一个代码示例:
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); } }
我知道我晚了一点,但我用veggerby的解决scheme,并build立了一个可能有用的帮手类,所以我想在这里提供:
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); } }
我提供了一个返回一个redirectActionResult
的静态方法和一个扩展了filterContext
的扩展方法。 希望有人认为这有用。
ObjectDictionary
是一个类,它使用reflection来创build一个字典,从它的构造对象的属性。 我没有包含这些代码,因为我相信在框架的某个地方有更好的方法。 我还没有find它,但我不希望别人inheritance我的潜在错误。
安全/授权/authenticationfilter应该使用AuthorizeAttribute和IAuthorizationFilter 。
public class IsGuestAttribute: AuthorizeAttribute, IAuthorizationFilter { public void OnResultExecuted(ResultExecutedContext filterContext) { } public void OnActionExecuting(ActionExecutingContext filterContext) { if (!Ctx.User.IsGuest) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" } }); } } }
用法: filterContext.RedirectToAction("Login", "Account");
这里有一个帮助类,我写了一些扩展方法写在更多的地方提供RedirectToActionfunction。 对于OP来说这太晚了,但是希望它能帮助别人!
public static class RedirectHelper { // RedirectToAction Extension Methods public static void RedirectToAction(this HttpResponseBase response, String action, String controller, object routeValues = null, bool endResponse = false) { response.RedirectToRoute(CreateRoute(action, controller, routeValues)); if (endResponse) response.End(); } public static void RedirectToAction(this HttpResponse response, String action, String controller, object routeValues = null, bool endResponse = false) { response.RedirectToRoute(CreateRoute(action, controller, routeValues)); if (endResponse) response.End(); } public static void RedirectToAction(this ActionExecutingContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false) { if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true); else filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues)); } public static void RedirectToAction(this ExceptionContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false) { if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true); else { filterContext.ExceptionHandled = true; filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues)); } } // Route Value Derivation public static RouteValueDictionary CreateRoute(String action, String controller, object routeValues = null) { RouteValueDictionary result = routeValues != null ? HtmlHelper.AnonymousObjectToHtmlAttributes(routeValues) : new RouteValueDictionary(); result["controller"] = controller; result["action"] = action; return result; } }
有更多的ControllerContexts不包括在内,但是根据你的需要添加你自己的控件应该相当容易。