asp.net MVC controller activation system Detailed: Overall design

Source: Internet
Author: User
Tags abstract httpcontext variables

We have divided the entire asp.net MVC framework into several subsystems, then the subsystem that activates the target controller object for the request context is our Controller activation system. Before formally discussing how the controller object is specifically created for love, let's look at the overall design of the controller activation system in asp.net mvc, and find out some of the basic components that make up the subsystem, and what their corresponding interfaces or abstract classes are.

First, Controller

We know that the IController interface is implemented directly or indirectly as a controller type. As the following code fragment shows, the IController interface only contains an Execute method with a parameter type of RequestContext. When a controller object is activated, the core operation is to resolve the target action method according to the request context, and to extract the corresponding data from the request context by model binding mechanism to the parameter of the method and finally execute the action method. All of these actions are invoked by the Execute method.

   1:public interface IController
2: {
3: void Execute (RequestContext requestcontext);
4:}

Execute, defined in the IController interface, is performed in a synchronized fashion. In order to support the processing of requests asynchronously, the asynchronous version System.Web.Mvc.IAsyncController of the IController interface is defined. As shown in the following code fragment, the execution of the asynchronous controller of the Iasynccontroller interface is done through a combination of beginexecute/endexecute methods.

   1:public Interface Iasynccontroller:icontroller
2: {
3: IAsyncResult beginexecute (RequestContext RequestContext, AsyncCallback callback, object state);
4: void EndExecute (IAsyncResult asyncresult);
5:}

The abstract class Controllerbase implements the IController interface, which has several important properties as follows. TemplateData, ViewBag, and ViewData are used to store data or variables that are passed from controller to view. where TemplateData and ViewData have a dictionary based data structure, key and value represent variable names and values, which are used to store variables based on the current HTTP context (the stored data is reclaimed after the current request is completed). ViewBag and ViewData have the same effect, even corresponding to the same data storage, the difference between them is that the former is a dynamic object, we can specify arbitrary attributes.

   1:public Abstract class Controllerbase:icontroller
2: {
3: //other Members
4: Public controllercontext ControllerContext {get; set;}
5: Public tempdatadictionary TempData {get; set;}
6: Public object ViewBag {[return:dynamic] get;}
7: Public viewdatadictionary ViewData {get; set;}
8:}

In asp.net mvc we will run into a series of context objects, which we have described in detail RequestContext (HttpContext + Routedata) that represent the request context. Now let's introduce another context type controllercontext with the following definition.

   1:public class ControllerContext
2: {
3: //other Members
4: Public controllercontext ();
5: Public controllercontext (RequestContext RequestContext, controllerbase Controller);
6: Public ControllerContext (HttpContextBase HttpContext,
7: routedata Routedata, controllerbase Controller);
8:
9: Public virtual controllerbase Controller {get; set;}
RequestContext RequestContext {get; set;}
One: Public virtual httpcontextbase HttpContext {get; set;}
: Public Virtual Routedata routedata {get; set;}
13:}

As the name suggests, ControllerContext is based on the context of a Controller object. As shown in the following code, ControllerContext is actually an encapsulation of a controller object and RequestContext, which correspond to the same name defined in the ControllerContext, respectively. And can be initialized in the constructor. The HttpContextBase and Routedata objects returned through the property HttpContext and Routedata properties are, by default, the core elements that make up the requestcontext. These four attributes of controllercontext are readable and writable, and we modify them arbitrarily. When the Controllerbase Execute method is executed, it creates the ControllerContext object based on the incoming Reuqestcontext, and subsequent operations can be viewed in that context.

When we are developing, the controller type created by vs default is actually inherited from the abstract class controller. Many of the helper methods and attributes are defined in the type to be programmed to be simple. As shown in the following code snippet, the controller type also explicitly implements the IController and Iasynccontroller interfaces, in addition to directly inheriting Controllerbase, and represents ASP.net MVC 4 Interfaces for four filters (Authorizationfilter, Actionfilter, Resultfilter, and Exceptionfilter).

   1:public Abstract class Controller:
2: controllerbase,
3: IController,
4: Iasynccontroller,
5: Iactionfilter,
6: Iauthorizationfilter,
7: Iexceptionfilter,
8: Iresultfilter,
9: IDisposable,
Ten:.
11: {
A: //Omit members
13:}

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.