The request life cycle of MVC

Source: Internet
Author: User

When an ASP. NET MVC application requests, in response to the request, contains some request execution process steps! HTTP request in ASP.

and the HTTP response process, there are 8 main steps involved:

1) Creation of the routetable (routing table)
2) UrlRoutingModule request interception
3) Routing engine determine route
4) Route handler create a related IHttpHandler instance
5) IHttpHandler instance to determine controller (director)
6) Controller Execution
7) A view engine is created
8) View Rendering

The main flowchart is as follows:

1) Creation of routetable

The creation of routetable occurs when the MVC application starts or the Web application pool restarts! A typical ASP. NET program, a page request corresponds to a page on the disk! such as (http://localhost/index.aspx
Corresponds to a file on the server disk index.aspx) index.aspx is actually a class that is instantiated by IHttpHandler. IHttpHandler contains a
ProcessRequest method, responsible for responding to page output!

But MVC application is different, each request maps to Route,route defined on the route table, created when the application starts!

The specific use of routetable in the application is as follows

 public class MvcApplication:System.Web.HttpApplication {public static VO ID registerroutes (routecollection routes) {routes.            Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. MapRoute ("Default",//Route name "{Controller }/{action}/{id} ",//URL with parameters new {controller =" Home ", action =" Ind            Ex ", id =" "}//Parameter defaults); Routes. MapRoute ("Account",//Route name "{controller}/ {Action}/{id} ',//URL with parameters new {controller = ' account ', action = "Log               On ", id =" "}//Parameter defaults);        } protected void Application_Start () {registerroutes (routetable.routes); }    }

2) UrlRoutingModule request interception

Each HTTP request is UrlRoutingModule intercepted and UrlRoutingModule provides the current HttpContext routing engine (the routing engine). The HttpContext instance contains all the data for the current request. UrlRoutingModule controls the routing engine, providing HttpContext data to routing engine! UrlRoutingModule implements the IHttpModule interface, which is registered in the Web. config file!

UrlRoutingModule specific data structures are as follows:

 public class Urlroutingmodule:ihttpmodule {//main Methods protected vir    tual void Init (HttpApplication application);    private void Onapplicationpostmaprequesthandler (object sender, EventArgs e);    private void Onapplicationpostresolverequestcache (object sender, EventArgs e);    public virtual void Postmaprequesthandler (HttpContextBase context);    public virtual void Postresolverequestcache (HttpContextBase context);    void Ihttpmodule.init (HttpApplication application);    Properties public routecollection routecollection {get; set;} } urlroutingmodule in Webconfig registration 

3) Routing engine determine route
Routing engine determines the processing of the route based on the current HttpContext. Routing engine indicates that the route table matches the route and creates the route processing in the Iroutehandler instance!

4) Route handler create a related IHttpHandler instance
In the route table, each route corresponds to a ihttphandler. IHttpHandler is responsible for creating a controller based on the current HttpContext data! IHttpHandler is created by the Gethttphandler of the currently active Iroutehandler!

Specific details are as follows

public interface iroutehandler{    //Methods    IHttpHandler gethttphandler (RequestContext requestcontext);}

5) IHttpHandler instance to determine controller (director)

In an MVC application, Mvchandler implements the Ihttphandler,controller instance, based on the input HttpContext and URL parameters corresponding to the route, Controllerfactory Create a controller,controllercontext containing the context data, passed into the controller's Excute method, triggering the controller's logical processing!

Mvchandler mainly has a controllerbuilder _controllerbuilder field;

Specific details are as follows

public class Mvchandler:ihttpasynchandler, IHttpHandler, irequiressessionstate{    //main fields    private Controllerbuilder _controllerbuilder;} Controllerbuilder class has a Main method Getcontrollerfactorypublic class controllerbuilder{public  icontrollerfactory Getcontrollerfactory ();} Create a controller by implementing the Icontrollerfactory factory

6) Controller execution
When all controller logic calls are executed, the actions request is executed! When the logic of the controller is executed, a actionresult is returned. A ActionResult instance, which triggers a view (view), is created and processed further when a trigger occurs.

7) A view engine is created
The instance of the view engine creates an instance of the IView interface, returning a Viewengineresult instance,

8) View Rendering
iview Instance compilation request view that provides data for the Render method call!

The request life cycle of MVC

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.