MVC源碼分析 - Action/Result 過濾器(續)

來源:互聯網
上載者:User

標籤: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 過濾器(續)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.