ASP. NET MVC life cycle

Source: Internet
Author: User

The ASP . NET application pipeline handles user requests with special emphasis on the " timing ", and how much of an understanding of the ASP's life cycle directly affects our efficiency in writing pages and controls. So in the year and the year I wrote an article on this topic:

    • "The log does not lie--asp.net life cycle"
    • "The diary does not lie the--asp.net life cycle" The Knot Question ""
    • "Two granularity to see the ASP. NET Life cycle"

For ASP. NET MVC, I'm still interested in its life cycle, so I ask two questions:

when an HTTP request is being handed over from IIS to the ASP. ASP, when was the time when net MVC gained control and processed the request ? What is the processing process ?

Taking the ASP . NET application life cycle in IIS7 as an example, is a schematic diagram of an event that occurred during an HTTP request processing from MSDN, followed by a complete list of events. Since ASP. NET MVC is based on the ASP. NET runtime, it is bound to intercept requests in the lifetime of the ASP . The first reaction, of course, is to go through the Web. config and we can see the UrlRoutingModule configuration section:

<add name= "UrlRoutingModule" type= "System.Web.Routing.UrlRoutingModule, System.Web.Routing, version=3.5.0.0, Culture=neutral, publickeytoken=31bf3856ad364e35 "/>

The following is a logical thing to do, Open the assembly with Reflector , you can see the following code:

Code

Seeing here our first question actually has the answer: the timing is in Postresolverequestcache and Postmaprequesthandler.

Resolverequestcache Event
Occurs when ASP. Finishes an authorization event to let the caching modules serve requests from the cache, bypassing ex Ecution of the event handler (for example, a-page or an XML Web service).

Source Document

Postmaprequesthandler Event
Occurs when ASP. Mapped the current request to the appropriate event handler.

Source Document

we use the ASP. NET MVC template in VS2008 to create a demo to complete the following discussion, what happens when we visit home/home?

    1. Request arrival
    2. IIS transfers processing rights to the ASP based on request characteristics
    3. UrlRoutingModule to match the current request in the Route table
    4. UrlRoutingModule finds the Routehandler in routecollection that the request matches, by default Mvcroutehandler Mvcroutehandler creates Mvchandler instances.
    5. Mvchandler executes ProcessRequest.
    6. Mvchandler uses Icontrollerfactory to obtain an instance of the IController interface to find the corresponding HomeController
    7. The index method of triggering homecontroller based on request
    8. Index to store execution results in ViewData
    9. The index method of HomeController returns ActionResult
    10. Views/home/index.aspx renders viewdata on the page
    11. Index.aspx Execute ProcessRequest method
    12. Index.aspx executing the Render method output to the client

By reading the source code of ASP. NET MVC, We can get a more detailed process , ignoring the details as much as possible, emphasizing the process of request processing. We cut in from the Global.asax.cs file , here is a sample code , here Initialize the routing table , please pay special attention to the comment section :

Code

Urlroutingmoudule Gets the routedata.routedata of the current request from routecollection in the Postresolverequestcache phase contains a request to handle the corresponding Controller and action,routedata This function through the processing of the request . Routedata extract Routehandler, here by default is Mvcroutehandler,mvcroutehandler get HttpHandler, The default here is Mvchandler.

Postresolverequestcache

Mvchandler. In ProcessRequest () , the HttpContext is first encapsulated using the Httpcontextwrapper , which is encapsulated for the purpose of understanding decoupling for testability. The Controller name is then extracted from the Requestcontext.routedata .
Controllerbuilder.getcontrollerfactory --controllerfactory. Createcontroller - icontroller.execute

The Controllerbase implements the IController interface , which, when Initialize, encapsulates the RequestContext as ControllerContext, Controller inherits from Controllerbase and implements abstract method Executecore ()

In Executecore, the controller first obtains actionname from the Routedata and then executes the actioninvoker.invokeaction.

In Actioninvoker we can see a variety of filter, which is an AOP practice: Several methods are executed before and after the action method executes. There are four kinds of filter:actionfilters,resultfilters, Authorizationfilters,exceptionfilters. These four kinds of filter are not closed, there are corresponding interfaces, these four are just the default implementation. The Filter is executed in the following order: Authorizationfilter--->action filter.onactionexecuting--->action Method---> The result returned by ActionFilter.OnActionExecuted.InvokeActionMethodWithFilters is ActionExecutedContext, Next, the controller executes the Onresultexecuting method. The result of ActionResult execution can be viewresult,jsonresult,redirectresult,contentresult, or a custom result type.

If the returned type is ViewResult, let's take a look at Viewreuslt's inheritance: Viewresult-->viewresultbase-->actionresult, Viewresult contains two attributes view and viewenginecollection, which is actually an implementation that contains two interfaces: Iviewengine defines how to position view/partial View.iview defines how to Renderview. The default implementation is Webformview and Webformviewengine.

Filter onresultexecuted The last step, you can catch the exception here. As we said, there are exceptionfilters, and if the exception in the previous procedure is not captured then it will eventually bubble to exceptionfilters.

    • Get ActionName in Routedata
    • Actioninvoker.invokeaction
    • Get Controllerdescriptor through ControllerContext
    • findaction-Get Actiondescriptor
    • Getfilters
    • Modelbinder convert the data in request to the parameters required by the action method
    • Authorizationfilter
    • Action filter.onactionexecuting
    • Action
    • actionfilter.onactionexecuted
    • Resultfilter.onresultexecuting
    • ActionResult execution
    • resultfilter.onresultexecuted
    • Webformviewengine.createview
    • Webformview.render
    • resultfilter.onexecuted

ASP. NET MVC life cycle

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.