MVC source code analysis, mvc source code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.