The core of the Execute method as a controller base class Controllerbase is the execution of the action method and the execution of the ActionResult returned as a method, The execution of both is done through a component called Actioninvoker.
First, Actioninvoker
We also define an interface Iactioninvoker for Actioninvoker. As shown in the following code snippet, the interface defines a unique method invokeaction the action method that executes the specified name, the first parameter of which is a ControllerContext object that represents the current controller context.
1:public Interface Iactioninvoker
2: {
3: void Invokeaction (ControllerContext controllercontext, string actionname);
4:}
The ControllerContext type is responsible for some of the true asp.net MVC framework, where we simplify it simply as a encapsulation of the current controller and request context, These two elements are represented by the controller and RequestContext properties shown below.
1:public class ControllerContext
2: {
3: Public controllerbase Controller {get; set;}
4: Public requestcontext RequestContext {get; set;}
5:}
An attribute of the same name in Controllerbase that represents Actioninvoker is initialized in the constructor. In the Execute method, the ControllerContext object is created by the RequestContext object as a method parameter, and the name of the target action is obtained by the routedata contained in the RequestContext. These two are then invoked as arguments to the Actioninvoker invokeaction method.
From the previous definition of controllerbase, we can see that the Actioninvoker created by default in the constructor is an object of type Controlleractioninvoker. The code snippet shown below reflects the definition of the entire controlleractioninvoker, and the Invokeaction method is designed to implement execution against the action method. Because the action method has the appropriate arguments, you must bind the parameters before executing the action method. asp.net mvc makes this mechanism the binding of model, and this involves another important component modelbinder.
1:public class Controlleractioninvoker:iactioninvoker
2: {
3:public Imodelbinder Modelbinder {get; private set;}
4:public Controlleractioninvoker ()
5: {
6:this. Modelbinder = new Defaultmodelbinder ();
7:}
8:public void Invokeaction (ControllerContext controllercontext, String actionname)
9: {
10:methodinfo method = ControllerContext.Controller.GetType (). GetMethods (). A (M => string.compare (ActionName, m.name, true) = = 0);
11:list<object> parameters = new list<object> ();
12:foreach (parameterinfo parameter in method. GetParameters ())
13: {
14:parameters. ADD (this. Modelbinder.bindmodel (controllercontext, parameter. Name, parameter. ParameterType));
15:}
16:actionresult ActionResult = method. Invoke (controllercontext.controller, parameters. ToArray ()) as ActionResult;
17:actionresult.executeresult (ControllerContext);
18:}
19:}
Second, Modelbinder
We provide a simple definition for modelbinder, which is different from the definition of an interface with the same name in true asp.net mvc. The interface has a unique Bindmodel object that takes a parameter based on the ControllerContext and the model name (which is actually the parameter name) and the type.
1:public Interface Imodelbinder
2: {
3:object Bindmodel (ControllerContext controllercontext, String modelname, Type modeltype);
4:}
We can see from the previous definition of controlleractioninvoker that the Modelbinder object created by default in the constructor is a Defaultmodelbinder object. Because it is just a simulation of asp.net mvc, the model binding logic defined in a custom defaultmodelbinder is much simpler to implement than the type in the same name in asp.net mvc.