在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
直接上代码,不要说话。
ASP.NET MVC4过滤器的简单应用:验证登录 1 [AcceptVerbs(HttpVerbs.Post)] 2 public ActionResult login(FormCollection form) 3 { 4 Session["login"] = "True"; 5 return Redirect("/Home/Index"); 6 7 }
1 [IsLogin] 2 public ActionResult Index() 3 { 4 return View(); 5 }
1 //自定义过滤器 2 public class IsLogin : ActionFilterAttribute 3 { 4 //当方法执行时 5 public override void OnActionExecuting(ActionExecutingContext filterContext) 6 { 7 //base.OnActionExecuting(filterContext); 8 try 9 { var sess=filterContext.RequestContext.HttpContext.Session["login"]; 10 if (sess==null ||sess.ToString()!="True") 11 filterContext.Result = new RedirectResult("/Home/login"); 12 } 13 catch 14 { 15 filterContext.Result = new RedirectResult("/Shared/Error"); 16 } 17 } 18 19 //当方法执行完毕 20 public override void OnActionExecuted(ActionExecutedContext filterContext) 21 { 22 base.OnActionExecuted(filterContext); 23 } 24 }
|
请发表评论