咱們來看兩個接口:css
public interface IActionFilter { // Methods void OnActionExecuted(ActionExecutedContext filterContext); void OnActionExecuting(ActionExecutingContext filterContext); }
public interface IResultFilter { // Methods void OnResultExecuted(ResultExecutedContext filterContext); void OnResultExecuting(ResultExecutingContext filterContext); }
這兩個接口都有兩個方法,分別表示視圖加載前,視圖加載後執行的方法。html
ViewResult執行前,執行後的執行的方法。ajax
咱們自定義一個過濾器ide
public class DemoFilter:ActionFilterAttribute { public string Message { get; set; } public override void OnActionExecuted(ActionExecutedContext filterContext) { //在Action執行以後執行 輸出到輸出流中文字:After Action execute xxx filterContext.HttpContext.Response.Write(@"<br />After Action execute" + "\t " + Message); base.OnActionExecuted(filterContext); } public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write(@"<br />Before Action execute"+"\t "+ Message); base.OnActionExecuting(filterContext); } public override void OnResultExecuted(ResultExecutedContext filterContext) { //在Result執行以後 filterContext.HttpContext.Response.Write(@"<br />After ViewResult execute" + "\t " + Message); base.OnResultExecuted(filterContext); } public override void OnResultExecuting(ResultExecutingContext filterContext) { //在Result執行以前 filterContext.HttpContext.Response.Write(@"<br />Before ViewResult execute" + "\t " + Message); base.OnResultExecuting(filterContext); }
咱們看看ActionFilterAttribute他爲何能重寫這幾個方法了:this
public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter { // Methods protected ActionFilterAttribute(); public virtual void OnActionExecuted(ActionExecutedContext filterContext); public virtual void OnActionExecuting(ActionExecutingContext filterContext); public virtual void OnResultExecuted(ResultExecutedContext filterContext); public virtual void OnResultExecuting(ResultExecutingContext filterContext); }
原來他實現了IActionFilter, IResultFilter 兩個接口spa
咱們來建立一個控制器,index方法,而且打上過濾標籤code
[DemoFilter] public ActionResult Index() { this.ControllerContext.HttpContext.Response.Write(@"<br />Action execute"); return Content("<br/>Result Excut! "); }
咱們執行這個視圖,視圖結果爲:htm
先執行視圖操做過濾,再執行ViewResult的過濾blog
固然咱們也能夠將這個過濾標籤打在Controller上面,這樣控制器下面的每一個視圖都會執行這個過濾操做繼承
[DemoFilter] public class HomeController : Controller
假如咱們想讓這個過濾執行兩次了,即在控制器上面執行一次,而後在視圖上面在執行一次,你會說,我在控制器上面打上一個過濾標籤,在視圖上面再打一個過濾標籤不就好了,可是結果仍是執行一次,解決這個,咱們知須要加上這句話:
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
這樣代碼:
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)] public class DemoFilter : ActionFilterAttribute
{
}
這樣咱們分別在homecontroller和視圖index上面打上標籤
[DemoFilter] public class HomeController : Controller { [DemoFilter] public ActionResult Index() { this.ControllerContext.HttpContext.Response.Write(@"<br />Action execute"); return Content("<br/>Result Excut! "); } }
會發現會執行兩次:
咱們有時候發現,這樣一個個的寫太麻煩了,想作一個全局過濾
咱們只須要在Application_Start()添加一個全局過濾
GlobalFilters.Filters.Add(new DemoFilter() { Message = "Gloable" });
這樣Application_Start()代碼爲:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalFilters.Filters.Add(new DemoFilter() { Message = "Gloable" }); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
咱們全部的控制器和視圖都不加過濾標籤,可是依舊執行了過濾。
標籤過濾:
好比有時候咱們須要判斷是ajax請求或者別的請求等咱們能夠本身加上一個標籤
標籤訂義爲:
public class AjaxRequest:Attribute { }
public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
ActionFilterAttribute繼承FilterAttribute,而FilterAttribute的父類是Attribute
public abstract class FilterAttribute : Attribute, IMvcFilter
{
}
咱們在DemoFilter的OnActionExecuting裏面判斷
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.ActionDescriptor.IsDefined(typeof(AjaxRequest), false)) { filterContext.Result = new ContentResult() { Content = "<br>直接跳過Action方法" }; } filterContext.HttpContext.Response.Write("不執行Action裏面的方法"); base.OnActionExecuting(filterContext); }
咱們在home控制器的index方法前面打上 [AjaxRequest]標籤
[AjaxRequest] public ActionResult Index() { this.ControllerContext.HttpContext.Response.Write(@"<br />Action execute"); return View(); }
再運行咱們發現結果爲:
和前面的比較
再也不執行action方法和onActionExcuted,可是會執行Result過濾器和生成視圖