使用ActionFilterAttribute进行重定向注意事项

1.分部视图方法不能添加该特性,会报子方法不能重定向操作的错误

2.必须用给filterContext.Result赋值的方法进行重定向,而不能用filterContext.HttpContext.Response.Redirect,

否则,该特性执行完成后,会继续执行Action和其他ActionFilterAttribute 

示例代码如下:

public class UserAuthenticationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext == null || filterContext.HttpContext == null)
                return;

            HttpRequestBase request = filterContext.HttpContext.Request;
            if (request == null)
                return;


            var workContext = EngineContext.Current.Resolve<IWorkContext>();

            if(workContext.CurrentSISTUser==null)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Account",
                    action = "Login"
                }));
                //filterContext.HttpContext.Response.Redirect("/Account/Login", true);
                return;
            }

        }
    }

来自:https://www.cnblogs.com/ldybyz/p/6236046.html

posted @ 2020-12-24 09:47  你也很优秀  阅读(210)  评论(0编辑  收藏  举报