[Go] This article only lets you understand the ASP. NET MVC principle

Source: Internet
Author: User

ASP. NET MVC is comprised of the following two core components:

    1. A custom HttpModule named UrlRoutingModule, used to parse the Controller and Action name;
    2. A custom HttpHandler named Mvchandler to enable activation of the Controller and execution of the Action;

!! Before reading this article, please understand the process of ASP and the role of HttpModule and HttpHandler.

The following is a simplified structure diagram of the related classes for routing conversions:

The routing information for the entire ASP. System is stored in the static variable routes (for a routedictionary type) of the Rotetable class, and the route is registered in Application_Start when the site starts running:

ROUTETABLE.ROUTES.ADD ("default",       new route{url="{ Controller}/{action}"});

When a URL request arrives, it is intercepted by UrlRoutingModule and the execution process is as follows:

    1. Encapsulates the current HTTP context and becomes a Httpcontextwrapper object.
    2. The Routedata object that conforms to the current request URL is obtained from Routes based on the current HTTP context. The object stores routehandler information.
    3. Encapsulates the routedata with an HTTP context request into a RequestContext object.
    4. Based on the RequestContext object, IHttpHandler is obtained from the Routehandler of the Routedata.
    5. Executes the IHttpHandler, making the real processing of the request.

Perform the timing diagram as shown:

The code for UrlRoutingModule is as follows:

1 New Httpcontextwrapper (httpcontext.current); 2 routedata routedata = RouteTable.Routes.GetRouteData (HttpContext); 3 New requestcontext{data = Routedata, context= HttpContext}; 4 IHttpHandler handler = RouteData.RouteHandler.GetHttpHandler (requestcontext); 5 Httpcontext.remaphandler (handler);

After the last step above, after executing the Httphandle, the program formally enters the Controller activation inside, the related class relation is as follows:

As with URL routing, when MVC is initialized, it also requires some information about registering the controller, which is to let the framework know what the default controller factory is, so in Application_Start:

ControllerBuilder.Current.SetControllerFactory (New Defaultcontrollerfactory ());

After the program passes the above URL route transformation, enters the Httphandle, after the following steps realizes to the Controller the activation:

    1. The Controller name is obtained from the Routedata in the RequestContext package.
    2. The current default Controller factory is obtained through Controllerbuilder.
    3. Based on the controller's name, the Director object is created (when Controllerfactory is initialized, all controller types that implement the IController interface are scanned throughout the assembly, so when Createcontroller is called, is actually obtained directly).
    4. Finally, the controller is executed. The essence of execution is the execution of Actioninvoker.invokeaction, which executes the corresponding Action according to the request context.

In the custom Mvchandler, the code is as follows:

 1  string  controllername =this  . Requestcontext.RouteData.Controller;  2  icontrollerfactory factory = ControllerBuilder.Current.GetControllerFactory ();  3   Get control (such as HomeController)  this   5  controller. Execute (this . RequestContext); 

A typical Iactioninvoker interface implements the Controlleractioninvoker Invokeaction method as follows:

1  Public voidinvokeaction (ControllerContext controllercontext,2                stringactionname)3 {4     //find the Action method5MethodInfo method =ControllerContext.Controller.GetType (). GetMethods ()6. First (m=>string. Compare (Actionname,m.name,true)==0);7     8     //get the action parameter and model binding9list<Object> Parameters =Newlist<Object>();Ten     foreach(ParameterInfo parameterinchmethod. GetParameters ()) One         { AParameters. ADD ( This. Modelbinder.bindmodel (ControllerContext, - parameter. Name, parameter. ParameterType)); -         } the  -     //execute action, and get ActionResult -ActionResult ActionResult =method. Invoke (Controllercontext.controller, -Parameters. ToArray ()) asActionResult; +  -     //finally actionresult the data back to the customer for display with HttpResponse + Actionresult.executeresult (controllercontext); A  at}
View Code

Eventually form an Http Response back to the client!!

The above is for me to tidy up the two core process of ASP. NET MVC, I hope the novice to see the understanding of the veteran more guidance of the problem, thank you!! There is time to continue releasing MVC's other core technologies. such as Model binding, data validation, and seriously to Artech Learning!!!!

[Go] This article only lets you understand the ASP. NET MVC principle

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.