標籤:ges config ted html img 控制 one href get
上一篇 看到了Action/Result過濾器的執行順序:
OnActionExecuting -> Action -> OnActionExecuted -> OnResultExecuting -> View-> OnResultExecuted
這一篇就來做幾個例子吧.
一、Demo
上一篇 的代碼可能並不怎麼好懂. 首先, 我能在FilterConfig中註冊過濾器, 可以在Controller中重寫Action/Result過濾器或者是在Controller上標註過濾器特性, 還可以在方法上加上過濾器特性. 那麼這些過濾器的執行, 都是在上一篇中的方法中執行的. 上一篇中, 並不能直觀的看到這些順序, 也是不好理解的點之一了.
但是上一篇提到過, 在調換順序之後, 最先執行的應該是Controller裡面的過濾器, 那麼到底是不是這樣呢?來看一下吧.
首先建幾個過濾器, 都繼承自 ActionFilterAttribute 類, 這裡面有Action/Result的四個過濾器方法
//FilterConfig中註冊使用public class MyFilterConfigAttribute : ActionFilterAttribute{ public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("FilterConfigAttribute - OnActionExecuted<br />"); } public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write("FilterConfigAttribute - OnActionExecuting<br />"); } public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("FilterConfigAttribute - OnActionExecuted<br />"); } public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("FilterConfigAttribute - OnActionExecuting<br />"); }}//Controller上標註此特性public class MyControllerAttribute : ActionFilterAttribute{ public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("ControllerAttribute - OnActionExecuted<br />"); } public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write("ControllerAttribute - OnActionExecuting<br />"); } public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("ControllerAttribute - OnActionExecuted<br />"); } public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("ControllerAttribute - OnActionExecuting<br />"); }}//Action上標註此特性public class MyActionAttribute : ActionFilterAttribute{ public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("ActionAttribute - OnActionExecuted<br />"); } public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write("ActionAttribute - OnActionExecuting<br />"); } public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("ActionAttribute - OnActionExecuted<br />"); } public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("ActionAttribute - OnActionExecuting<br />"); }}
然後就是在控制器和視圖.
[MyController]public class FootController : Controller{ protected override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("FootController - OnActionExecuted<br />"); } protected override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write("FootController - OnActionExecuting<br />"); } protected override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("FootController - OnResultExecuted<br />"); } protected override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("FootController - OnResultExecuting<br />"); } [MyAction] public ActionResult Get() { Response.Write("<br /><br />Action 方法被執行<br /><br />"); return View(); }}
@{ ViewBag.Title = "Get";}<br /><br />Get - View視圖被解析<br /><br />
準備妥當了, 上結果:
從以上結果中可以看出, 最先執行的, 是Controller裡面的ActionExecuting方法.
這裡的執行順序, 有點類似遞迴的執行過程. 從這個結果來看上一篇的執行過程, 就很清晰了.
Controller內部Action/Result過濾器 - > FilterConfig註冊Action/Result過濾器 - > Controller標註Action/Result特性 - > Action標註Action/Result特性
目錄已同步
MVC源碼分析 - Action/Result 過濾器(續)