Next, let's talk about other functions of controller.
The first is the imodelbinder interface. It has only one method:
Public interface imodelbinder {
Object bindmodel (controllercontext, modelbindingcontext bindingcontext );
}
This method can convert user input (Form, route, querystring) into our custom class. ASP. net mvc comes with a model Binder: defaultmodelbinder. It can convert the input value to the native type of. net, or even the ilist type. But if we need to convert the input to a custom type, we need to extend it. I am not very clear about the specific usage. I still need to continue to explore it. However, once a model binder is customized, it must be registered in globle. asax.
Onactionexecuted (actionexecutedcontext filtercontext );
Onauthorization (authorizationcontext filtercontext );
Onresultexecuted (resultexecutedcontext filtercontext );
Onexception (exceptioncontext filtercontext );
Then, let's talk about the filter of the controller. The filter function inserts your own logic code during ASP. net mvc requests. Four interfaces are used for the filter function: iactionfilter, iresultfilter, iauthorizationfilter, and iexceptionfilter.
Public interface iactionfilter {
Void onactionexecuting (actionexecutingcontext filtercontext );
Void onactionexecuted (actionexecutedcontext filtercontext );
}
Public interface iauthorizationfilter {
Void onauthorization (authorizationcontext filtercontext );
}
Public interface iresultfilter {
Void onresultexecuting (resultexecutingcontext filtercontext );
Void onresultexecuted (resultexecutedcontext filtercontext );
}
Public interface iexceptionfilter {
Void onexception (exceptioncontext filtercontext );
}
We can see that these methods are like page lifecycle events that are familiar to webform. The execution sequence of these methods is: onauthorization-> onactionexecuting-> onactionexecuted-> onresultexecuting-> onresultexecuted-> onexception. Of course, the last onexception is not fixed, because once an exception occurs, the program will immediately execute this method.
The Controller class has implemented these interfaces, so we can insert the custom logic by overwriting these methods. However, another better method is to use filterattrbute to insert custom logic. One major benefit of doing so is that we can perform logical operations on an action without having to operate all the actions of the entire controller. By default, MVC provides several attributes. We can also write our own attribute. The syntax is: 1. inherit filterattribute 2. Implement the corresponding interface (the four mentioned above ).
There are still a lot to go into about controller. I will try again later.