1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace lizhugz.Areas.lizhuAdmin.Models 8 { 9 public class FilterAttribute:ActionFilterAttribute 10 { 11 /* Filter 12 * 体验AOP编程思想 13 Result:视图 14 Ation:方法 15 用[FilterAttribute]调用 16 放在控制器前:该控制器下所有方法经过这个验证 17 放在方法前:该方法使用这个验证 18 */ 19 public string Message { get; set; } 20 21 //在Action执行之后执行 22 public override void OnActionExecuted(ActionExecutedContext filterContext) 23 { 24 25 base.OnActionExecuted(filterContext); 26 } 27 28 //在Action执行之前执行 29 public override void OnActionExecuting(ActionExecutingContext filterContext) 30 { 31 if (HttpContext.Current.Session["userId"]==null) 32 { 33 34 HttpContext.Current.Response.Redirect("/lizhuAdmin/AdeminProData/Login"); 35 } 36 else 37 { 38 HttpContext.Current.Response.Redirect("/lizhuAdmin/AdeminProData/Index"); 39 } 40 41 42 base.OnActionExecuting(filterContext); 43 44 45 } 46 47 //在Result执行之后执行 48 public override void OnResultExecuted(ResultExecutedContext filterContext) 49 { 50 base.OnResultExecuted(filterContext); 51 } 52 53 //在Result执行之前 54 public override void OnResultExecuting(ResultExecutingContext filterContext) 55 { 56 base.OnResultExecuting(filterContext); 57 } 58 } 59 60 61 62 }