简介
mvc的过滤器类型有四种,分别如下:
过滤器类型 | 接口 | 默认实现 | 描述 |
---|
Action | IActionFilter | ActionFilterAttribute | 在动作方法之前及之后运行 | Result | IResultFilter | ActionFilterAttribute | 在动作结果被执行之前和之后运行 | AuthorizationFilter | IAuthorizationFilter | AuthorizeAttribute | 首先运行,在任何其它过滤器或动作方法之前 | Exception | IExceptionFilter | HandleErrorAttribute | 只在另一个过滤器、动作方法、动作结果弹出异常时运行 |
Demo 下载
链接:https://pan.baidu.com/s/1q6Cxw0-Ea1lFbu3_cBl8UA 提取码:59vn
Action、Result
新建一个MyCustormFilter类, 该类继承 ActionFilterAttribute,可以重写父类的 OnActionExecuting、OnActionExecuted、OnResultExecuting、OnResultExecuted等方法,具体含义如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Demo.App_Start
{
public class MyCustormFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("Action执行前<br />");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("Action执行后<br />");
base.OnActionExecuted(filterContext);
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("加载视图前执行 OnResultExecuting <br/>");
base.OnResultExecuting(filterContext);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("加载视图后执行 OnResultExecuted <br/>");
base.OnResultExecuted(filterContext);
}
}
}
Index标记MyCustormFilter特性
[MyCustormFilter]
public ActionResult Index()
{
return View();
}
AuthorizeAttribute
新建一个TestAuthorizeAttribute类, 该类继承 AuthorizeAttribute,可以重写父类的 OnAuthorization等方法,具体含义如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Demo.App_Start
{
public class TestAuthorizeAttribute: AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.HttpContext.Response.Write("授权过滤器 --在Action过滤器前执行<br/>");
}
}
}
给Index加上TestAuthorizeAttribute特性
[TestAuthorizeAttribute]
[MyCustormFilter]
public ActionResult Index()
{
return View();
}
运行结果如下图
Exception
新建一个TestHandleError类, 该类继承 HandleErrorAttribute,可以重写父类的 OnException等方法,具体含义如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Demo.App_Start
{
public class TestHandleError: HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
Exception ex = filterContext.Exception;
filterContext.Result = new RedirectResult("~/error.html");
filterContext.ExceptionHandled = true;
base.OnException(filterContext);
}
}
}
在Action上面加TestHandleError
通常这样的异常处理我们是放在全局过滤器里面
例如:
using Demo.App_Start;
using System.Web;
using System.Web.Mvc;
namespace Demo
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new TestHandleError());
}
}
}
|