有时项目要进行客户端请求(action)进行拦截(过滤)验证等业务,可以使用拦截器进行实现,所谓的action拦截器也没有什么的,只是写一个类,继承另一个类(System.Web.Mvc.FilterAttribute)和一个接口(System.Web.Mvc.IActionFilter),至于什么是拦截器这里就不说了,网上很多关于这方面文章。
假如现在有这样的一个需求:某个action需要登录才能进行访问,可以使用action属性拦截器进行拦截进行验证
多余的不说了直接上代码
写一个拦截器类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace AttributeDemo.Common 8 { 9 ///10 /// Action拦截器11 /// 12 public class ActionFilterAttribute : System.Web.Mvc.FilterAttribute, System.Web.Mvc.IActionFilter13 {14 #region 属性15 ///16 /// 记录是否登陆17 /// 18 public bool IsLogin { get; set; }19 #endregion20 21 #region 执行action后执行这个方法22 ///23 /// 执行action后执行这个方法24 /// 25 /// 26 void System.Web.Mvc.IActionFilter.OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)27 {28 29 }30 #endregion31 32 33 #region 执行action前执行这个方法 34 ///35 /// 执行action前执行这个方法36 /// 37 /// 38 void System.Web.Mvc.IActionFilter.OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)39 {40 41 if (!this.IsLogin) //未登陆 重定向 到登陆页面42 {43 if (filterContext.HttpContext.Request.IsAjaxRequest()) //判断是否ajax请求44 {45 46 filterContext.Result = new System.Web.Mvc.JsonResult() { Data = new { statusCode = 301 }, ContentEncoding = System.Text.Encoding.UTF8, JsonRequestBehavior = JsonRequestBehavior.AllowGet, ContentType = "json" };47 return;48 }49 else //验证不通过50 {51 //filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "ActionFilterTest", action = "Login" })); //重定向52 //filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new Dictionary() { { "controller", "ActionFilterTest" }, { "action", "Login" } })); //重定向53 54 //filterContext.Result = new System.Web.Mvc.RedirectToRouteResult("Default", new System.Web.Routing.RouteValueDictionary(new Dictionary () { { "controller", "ActionFilterTest" }, { "action", "Login" } })); //重定向55 56 //filterContext.Result = new System.Web.Mvc.RedirectToRouteResult("Default", new System.Web.Routing.RouteValueDictionary(new Dictionary () { { "controller", "ActionFilterTest" }, { "action", "Login" } }),true); //重定向57 58 filterContext.Result = new System.Web.Mvc.RedirectToRouteResult("MyRoute", new System.Web.Routing.RouteValueDictionary(new Dictionary () { { "controller", "ActionFilterTest" }, { "action", "Login" } }), true); //重定向59 return;60 }61 62 }63 64 }65 #endregion66 67 }68 }
请求的控制器类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace AttributeDemo.Controllers 8 { 9 ///10 /// 测试Action拦截器11 /// 12 public class ActionFilterTestController : Controller13 {14 //15 // GET: /ActionFilterTest/16 17 ///18 /// 测试action拦截器19 /// 20 ///21 [AttributeDemo.Common.ActionFilter(IsLogin = false)]22 public ActionResult Test()23 {24 return View();25 }26 27 28 public ActionResult Login()29 {30 return View();31 }32 33 [AttributeDemo.Common.ActionFilter(IsLogin = true)]34 public ActionResult Index()35 {36 return View();37 }38 }39 }
Global.asax类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Routing; 7 8 namespace AttributeDemo 9 {10 // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,11 // 请访问 http://go.microsoft.com/?LinkId=939480112 13 public class MvcApplication : System.Web.HttpApplication14 {15 public static void RegisterGlobalFilters(GlobalFilterCollection filters)16 {17 filters.Add(new HandleErrorAttribute());18 }19 20 public static void RegisterRoutes(RouteCollection routes)21 {22 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");23 24 routes.MapRoute(25 "Default", // 路由名称26 "{controller}/{action}/{id}", // 带有参数的 URL27 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值28 );29 routes.MapRoute(30 "MyRoute", // 路由名称31 "{controller}/{action}/{id}", // 带有参数的 URL32 new { controller = "ActionFilterTest", action = "Login", id = UrlParameter.Optional } // 参数默认值33 );34 }35 36 protected void Application_Start()37 {38 AreaRegistration.RegisterAllAreas();39 40 RegisterGlobalFilters(GlobalFilters.Filters);41 RegisterRoutes(RouteTable.Routes);42 }43 }44 }
注意:拦截器也可以写在控制器类属性上,写在控制器类上表示拦截该控制器下所有action,如果写在action只是拦截当前action有效
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace AttributeDemo.Controllers{ ////// 测试Action拦截器 /// [AttributeDemo.Common.ActionFilter(IsLogin = false)] public class ActionFilterTestController : Controller { // // GET: /ActionFilterTest/ }}