MVC two must know the core

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:

Httpcontextwrapper HttpContext = new Httpcontextwrapper (httpcontext.current); Routedata Routedata = RouteTable.Routes.GetRouteData (HttpContext); RequestContext RequestContext = new requestcontext{data = Routedata, context= httpcontext};ihttphandler handler = RouteDa Ta. Routehandler.gethttphandler (RequestContext); 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. The controller object is created by reflection, according to the controller's name.
    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:

String Controllername =this. Requestcontext.routedata.controller;icontrollerfactory factory = ControllerBuilder.Current.GetControllerFactory () ; IController Controller = Controllerfactory.createcontroller (this. Requestcontext,controllername); controller. Execute (this. RequestContext);

MVC two must know the core

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.