How asp.net MVC works (3): Controller activation

Source: Internet
Author: User
Tags http request

asp.net MVC url Routing system resolves HTTP requests through registered routing tables to obtain a Routedata object for encapsulating routing data. This process is accomplished by registering the HttpApplication Postresolverequestcache event with a custom urlroutingmodule. Routedata already contains the name of the target controller, let's further analyze how the real controller object is activated. We first need to understand a type with a type of mvcroutehandler.

First, Mvcroutehandler

We know from the previous introduction that the route type inherited from RouteBase has a property of type Iroutehandler interface Routehandler, Its main purpose is to obtain a HttpHandler object based on the specified request context (represented by a RequestContext object). When the Getroutedata method is executed, the value of the route's Routehandler property is reflected on the resulting Routedata property of the same name. By default, the route Routehandler property is a Mvcroutehandler object, as the following code fragment reflects this.

   1:public class Route:routebase
2: {
3: //other Members
4: Public iroutehandler Routehandler {get; set;}
5: Public Route ()
6: {
7: //Other operations
8: This . Routehandler = new Mvcroutehandler ();
9: }
10:}

For our mini version of the ASP.net MVC framework, Mvcroutehandler is a type with the following definitions. In the Gethttphandler method of implementation, it returns a Mvchandler object directly.

   1:public class Mvcroutehandler:iroutehandler
2: {
3: Public IHttpHandler Gethttphandler (RequestContext requestcontext)
4: {
5: Return new Mvchandler (RequestContext);
6: }
7:}

Second, Mvchandler

As we have mentioned in the previous section, the entire ASP.NET MVC framework is implemented through a custom HttpModule and HttpHandler object asp.net extension. This custom HttpModule we have already introduced, is UrlRoutingModule, and this custom HttpHandler is we want to focus on Mvchandler.

UrlRoutingModule after parsing the HTTP request through the routing table to get a routedata to encapsulate the routing data, or call its Routehandler Gethttphandler method to get the HttpHandler object and register it with the current HTTP context. Because the routehandler of routedata originates from the routehandler of the corresponding route object, which is a Mvcroutehandler object by default, So this is the Mvchandler object that is used to handle HTTP requests by default. Mvchandler implements the activation of the Controller object and the execution of the corresponding action method.

The following code fragment embodies the entire definition of Mvchandler, which has a property of type RequestContext representing the current request context being processed, which is specified in the constructor. The activation and execution of the Controller object is realized in the implementation of the ProcessRequest.

   1:public class Mvchandler:ihttphandler
2: {
3: Public bool IsReusable
4: {
5: Get{return false;
6: }
7: Public requestcontext RequestContext {get; private set;}
8: Public Mvchandler (RequestContext requestcontext)
9: {
A: this . RequestContext = RequestContext;
One: }
: Public void ProcessRequest (HttpContext context)
: {
string controllername = this. RequestContext.RouteData.Controller;
: icontrollerfactory controllerfactory = ControllerBuilder.Current.GetControllerFactory ();
: IController controller = Controllerfactory.createcontroller (this. RequestContext, controllername);
: Controller. Execute (this. RequestContext);
: }
19:}

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.