MVC source code analysis, mvc source code
In the previous article, we saw the execution sequence of the Action/Result filter:
OnActionExecuting-> Action-> OnActionExecuted-> OnResultExecuting-> View-> OnResultExecuted
Here are a few examples.
I. Demo
The previous Code may not be easy to understand. first, I can register a filter in FilterConfig. I can rewrite the Action/Result filter in the Controller, mark the filter feature on the Controller, and add the filter feature to the method. these filters are all executed in the method in the previous article. in the previous article, we couldn't intuitively see these sequences, which is also one of the hard-to-understand points.
But as mentioned in the previous article, after changing the order, the first filter in the Controller should be executed. Is that true? Let's take a look.
First, several filters are created and inherited from the ActionFilterAttribute class. There are four filter methods for Action/Result.
// Register and use 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/>"); }}// mark this feature on the 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/>");} // mark this feature on the 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/> ");}}
Then it is in the Controller and view.
[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/> Action method executed <br/>"); return View ();}}
@ {ViewBag. Title = "Get" ;}< br/> <br/> Get-View parsed <br/>
The answer is:
From the above results, we can see that the first execution is the ActionExecuting method in the Controller.
The execution sequence here is a bit similar to the Recursive Execution process. From this result, we can see that the execution process in the previous article is very clear.
Controller internal Action/Result filter-> FilterConfig register Action/Result filter-> Controller annotation Action/Result feature-> Action annotation Action/Result feature
Directory synchronized