ASP. NET has no magic-instantiation and execution of ASP. net mvc Controller, mvccontroller
The previous chapter introduced the process of routing registration and matching, knowing that MVC Http requests are finally handled by MvcHandler, and the process is to create, execute, and release the Controller.
This chapter will further introduce the three procedures mentioned above from the following points:
● MvcHandler Overview
● ControllerBuilder & ControllerFactory
● DefaultControllerFactory
● Controller & ControllerBase
● Controller execution
MvcHandler Overview
MvcHandler is the core component of MVC and is used to receive and process MVC requests from customers. The following is a definition of the MvcHandler type:
MvcHandler implements the IHttpHandler and asynchronous IHttpAsyncHandler interfaces, which are used to process Http requests, in fact, the so-called "processing" is to generate a response based on the user request information and return it to the client. Therefore, in Mvc, the required data is captured through the Head and Body information of the Http request, and then transmitted to the Controller Action to execute the business logic, render an Html template and return it to the client.
Its construction requires a RequestContext object. In fact, RequestContext is the encapsulation of HttpContextBase (including all the information of the HTTP request) and RouteData. This object is created in UrlRoutingModule and then passed to MvcHandler through MvcRouteHandler.
The following figure shows the processing overview of MvcHandler:
ControllerBuilder & ControllerFactory
One of the steps executed by MvcHandler is initialization. The most important thing is to create a Controller, and the creation of a Controller is inseparable from ControllerBuilder and ControllerFactory. What do they do?
1. ControllerBuilder: used to obtain and set ControllerFactory. DefaultControllerFactory is obtained by default if ControllerFactory is not set.
The following is the definition of ControllerBuilder:
2. ControllerFactory: it is actually an IControllerFactory interface that is used to create and destroy controllers. The default Implementation of MVC is DefaultControllerFactory.
To sum up, ControllerBuilder is used to create ControllerFactory, while ControllerFactory is used to create Controller. The default Implementation of ControllerFactory is defacontrocontrollerfactory.
DefaultControllerFactory
The following is the definition of DefaultControllerFactory:
In addition to the IControllerFactory method, it also has two protected Virtual Methods named GetControllerInstance and GetControllerType. In addition, the constructor has a parameter of the IControllerActivator type. The former is easy to understand. Net allows you to create an instance using a type, and you can find the corresponding type in the set by the type name. Therefore, we can think of two methods: getting the type based on the Controller name and then creating an instance through the type. The latter is the Controller activator from the perspective of name. What is its role? Is it used to create a Controller instance?
The following is the CreateController Code. The Code also confirms the above speculation. The Controller is created by first finding the type based on the Controller name, and then creating the Controller instance through the type.
1. Search for the Controller type:
1 ). namespace processing: The namespace in the route, the namespace in ControllerBuilder, and no namespace. The three namespaces match with their values as namespaces in sequence, until the corresponding Controller type is found. In other words, if both the route object and the Controller Builder have a namespace set, first query the route object. If no namespace is found, use the namespace set by ControllerBuilder, if it still does not exist, the namespace will not be used for matching.
2 ). controller-type cache data processing: To improve MVC performance, retrieving the IController type in all dependent assemblies generates a cache file named "MVC-ControllerTypeCache.xml" (completed by the ControllerTypeCache type) when you first look up the Controller type in a cache ), the cache file format is as follows:
Note: The cache path is generally in systemroot \ Microsoft. NET \ Framework \ versionNumber \ Temporary ASP. NET Files \ {app name }\.. \.. \ UserCache or systemroot \ Microsoft. NET \ Framework \ versionNumber \ Temporary ASP. NET Files \ root \.. \.. \ UserCache directory, but may vary depending on the Web host or environment configuration, but the HttpRuntime can be used in the code. find the CodegenDir attribute. There is also a cache file that is registered for Area, named MVC-AreaRegistrationTypeCache.xml.
3). type Matching: The type Matching of Controller is relatively simple. It uses the {NameSpace}. {ControllerName} format to match in the data structure of the above file.
2. Create a Controller:
1) Determine whether the Controller type inherits from IController.
2) create a Controller instance through ControllerActivator.
3. ControllerActivator: used to create a Controller. In MVC, A DefaultControllerActivator exists in DefaultControllerFactory and is used to create a Controller instance through the Activator object or dependency parser in. Net.
Controller & ControllerBase
When developing an MVC Controller, the Controller type is generally used as the base class and inherited. In this way, you can easily obtain the functions provided by the Controller base class. Controller actually inherits from the ControllerBase abstract class. ControllerBase provides the most core Execute method and ExecuteCore abstract method in MVC and some necessary attributes such as TempDataDictionary and ViewDataDictionary, the typical template Pattern Defines the Controller framework.
1. ControllerBase: defines the Controller core framework
2. controller: Implements ASP.. net mvc controller execution logic and additional functions, such as verification, exception handling, event processing (Action execution event, verification event, etc), jump, multiple return types (including but not limited to MVC pages, Json data, files, and common content.
Note: Since the Controller has too many defined code, it is not released. You can view it in.
Controller execution
As mentioned above, ControllerBase is the template of the entire Controller class structure. net mvc calls the Controller to Execute the Execute method of the ControllerBase type. The method actually calls the ExecuteCore method of the subclass. The following is the ExecuteCore of the Controller type:
From the code, we can see that the execution of the Controller is very simple. There are only four steps: loading temporary data, obtaining actions, executing actions, and saving temporary data.
Note: This chapter will not introduce the execution of the Action, because the execution of the Action contains a lot of content, such as the execution of the filter and the binding of the model, so these content will be explained later.
Summary
This chapter takes MvcHandler as the entry, creates and executes MVC controllers as the process, and introduces important objects and execution logic. For more information, see subsequent articles.
Link: http://www.cnblogs.com/selimsong/p/7677108.html
ASP. NET has no magic-directory