MVC 全局过滤器

1. 新创建一个类 CheckLogin
2. 在类中加入以下代码

 public class CheckLogin : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Session["RealName"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Home", Action = "ErrorView" }));

                return;
             
            }
        }


    }

3. 在控制器中需要引用的方法前引用
[CheckLogin]

猜你喜欢

转载自www.cnblogs.com/lyq666666/p/9455846.html