NET MVC过滤器
本系列目录:ASP.NET MVC4入门到精通系列目录汇总
在ASP.NET MVC中有四种过滤器类型
Action
1、在ASP.NET MVC项目中,新建文件夹Filter,然后新建类MyCustormFilter,继承自ActionFilterAttribute类,我们来看下ActionFilterAttribute类有如下四个方法,从命名我应该就可以看出他们的执行时机。
public class MyCustormFilter:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write("Action执行前"); base.OnActionExecuting(filterContext); } public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("Action执行后"); base.OnActionExecuted(filterContext); } }
2、对于过滤器,我们可以把它们加在三个地方,一个是控制器上面(控制器下面的所有Action),一个是Action上面(指定标识的Action),另一个就是全局位置(所有控制器中的Action)。这里我只演示在Action上面加
[MyCustormFilter]
public ActionResult Index()
{
return View();
}
public ActionResult Index1()
{
return View();
}
3、build然后运行
Result
1、新建类TestResultFilter,继承ActionFilterAttribute
public class TestResultFilter:ActionFilterAttribute { /// <summary> /// 加载 "视图" 前执行 /// </summary> /// <param name="filterContext"></param> public override void OnResultExecuting(System.Web.Mvc.ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("加载视图前执行 OnResultExecuting <br/>"); base.OnResultExecuting(filterContext); } /// <summary> /// 加载"视图" 后执行 /// </summary> /// <param name="filterContext"></param> public override void OnResultExecuted(System.Web.Mvc.ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("加载视图后执行 OnResultExecuted <br/>"); base.OnResultExecuted(filterContext); } }
2、这里我把TestResultFilter过滤器加在控制器上面
[TestResultFilter] public class FilterTestController : Controller { [MyCustormFilter] public ActionResult Index() { return View(); } public ActionResult Index1() { return View(); } }
3、运行看下结果
注意:Result过滤器,无论Action的返回类型为什么(甚至void)都将执行。
RouteData中保存了当前请求匹配的路由信息和路由对象
修改MyCustormFilter.cs
public override void OnActionExecuting(ActionExecutingContext filterContext) { //1.获取获取请求的类名和方法名 string strController = filterContext.RouteData.Values["controller"].ToString(); string strAction = filterContext.RouteData.Values["action"].ToString();//2.另一种方式 获取请求的类名和方法名 string strAction2 = filterContext.ActionDescriptor.ActionName; string strController2 = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; filterContext.HttpContext.Response.Write("Action执行前</br>"); filterContext.HttpContext.Response.Write("控制器:" + strController + "</br>"); filterContext.HttpContext.Response.Write("控制器:" + strController2+"</br>"); filterContext.HttpContext.Response.Write("Action:" + strAction + "</br>"); filterContext.HttpContext.Response.Write("Action:" + strAction2 + "</br>"); base.OnActionExecuting(filterContext); }
AuthorizeAttribute
1、 新建TestAuthorizeAttribute
/// <summary> /// 授权过滤器 --在Action过滤器前执行 /// </summary> public class TestAuthorizeAttribute:AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { filterContext.HttpContext.Response.Write("<br/>OnAuthorization<br/>"); //注释掉父类方法,因为父类里的 OnAuthorization 方法会调用asp.net的授权验证机制! //base.OnAuthorization(filterContext); } }
2、在控制器FilterTest中的Index上添加TestAuthorize标记
[MyCustormFilter] [TestAuthorize] public ActionResult Index() { return View(); }
运行看下结果:
Exception
1、新建TestHandleError.cs
/// <summary> /// 异常处理 过滤器 /// </summary> public class TestHandleError : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { //1.获取异常对象 Exception ex = filterContext.Exception; //2.记录异常日志 //3.重定向友好页面 filterContext.Result = new RedirectResult("~/error.html"); //4.标记异常已经处理完毕 filterContext.ExceptionHandled = true; base.OnException(filterContext); } }
2、在Action上面加TestHandleError
[TestHandleError] public ActionResult GetErr() { int a = 0; int b = 1 / a; return View(); }
注意:通常这样的异常处理我们是放在全局过滤器上面的。
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //filters.Add(new HandleErrorAttribute()); //添加全局过滤器 filters.Add(new TestHandleError()); } }