using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace OAMvcApp.Models { public class LoginCheckFilterAttribute:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (filterContext.HttpContext.Session["user"] == null) { filterContext.HttpContext.Response.Redirect("/Login/Index"); } } } }
控制器使用方法:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using OAMvcApp.Models; namespace OAMvcApp.Controllers { [LoginCheckFilterAttribute] public class HomeController : Controller { public ActionResult Index() { return View("win7"); } public ActionResult About() { ViewBag.Message = "Your app description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } }