RedirectToAction
只是一个构造a的辅助方法RedirectToRouteResult()
,所以你所做的只是创建一个新的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); } }