ASP. NET adopts a pipeline design and has good scalability. The entire ASP. net mvc application framework is implemented by extending ASP. NET. ASP. net pipeline design, we know ASP. as long as the extension points of net are embodied in the two core components of httpmoudle and httphandler, the whole ASP. net MVC framework is implemented through custom httpmoudle (urlroutingmodule) and httphandler (mvchandler. For readers to grasp ASP as a whole. net MVC working mechanism. Next, I use some custom components to simulate ASP. net MVC operating principle, we can also regard this as a "mini version" ASP. net MVC. It is worth mentioning that in order for the reader to find the corresponding component from the real ASP. net mvc Based on the instance, I completely adopt the type naming method consistent with ASP. NET MVC. [Source codeDownload from here]
Before officially introducing the implementation principle of the "mini version" ASP. net mvc we have created, let's take a look at how Web applications built on this framework are implemented. We use Visual Studio to create an empty ASP. NET web application (note that it is not an ASP. net mvc application). We will not reference system. Web. MVC. dll.ProgramSo the type of the same name that you see in the next program in this Assembly is customized.
First, we define the next simplemodel type, which indicates the data to be bound to the view. For simplicity, to verify the resolution mechanism for the Controller and action, simplemodel defines two attributes to indicate the target controller and action of the current request, respectively.
1: Public ClassSimplemodel
2:{
3:Public StringController {Get; set ;}
4:Public StringAction {Get; set ;}
5:}
Like ASP. net mvc application development, we need to define the Controller type. See the followingCodeAs shown in the section, according to the naming method we are familiar with (with the character controller as the suffix), we define the next homecontroller. The abstract type controllerbase implemented by homecontroller is defined by us. The custom actionresult is used as the index method of the return type to indicate the action of the controller. It accepts a simplemodel type object as the parameter. The actionresult returned by this action method is a rawcontentresult object. As the name suggests, rawcontentresult displays the specified content as is. Here we will display the controller and Action attributes of simplemodel objects as parameters.
1: Public ClassHomecontroller: controllerbase
2:{
3:PublicActionresult index (simplemodel Model)
4:{
5:StringContent =String. Format ("Controller: {0} <br/> action: {1 }", Model. Controller, model. action );
6:Return NewRawcontentresult (content );
7:}
8:}
ASP. net mvc parses the type and Action Method Name of the controller used to process the request based on the request address. Specifically, we pre-register some (relative) Address templates that contain the controller and action names as the placeholder. If the request address conforms to the corresponding address template mode, the Controller and action names can be correctly parsed. Similar to ASP. net mvc applications, we have registered the following address template ({controller}/{action}) in global. asax }).
1: Public ClassGlobal: system. Web. httpapplication
2:{
3:Protected VoidApplication_start (ObjectSender, eventargs E)
4:{
5:Routetable. routes. Add ("Default",NewRoute {url ="{Controller}/{action }"});
6:Controllerbuilder. Current. setcontrollerfactory (NewDefaultcontrollerfactory ());
7:Controllerbuilder. Current. defaultnamespaces. Add ("Webapp");
8:}
9:}
In the application_start method used to register an address template as shown above, we also registered a factory for creating a controller object. The previously defined homecontroller is defined under the namespace webapp. Because the request address can only parse the name of the controller type, we need to register the namespace as the default namespace of the current controllerbuilder. Routetable, controllerbuilder, and defaultcontrollerfactory are all custom types.
As I mentioned above, ASP. NET MVC is implemented through a custom httpmodule. In this "mini version" ASP. NET MVC framework, we also name it urlroutingmodule. Before running a web application, we need to register the custom httpmodule through configuration. The following describes the relevant configuration.
1: <Configuration>
2:<System. Webserver>
3:<Modules>
4:<Add Name= "Urlroutingmodule"
5:Type= "Artech. minimvc. urlroutingmodule, artech. minimvc"/>
6:</Modules>
7:</System. Webserver>
8: </Configuration>
So far, all programming and configuration work has been completed. In order to let the action method index defined in homecontroller process access requests for this web application, we need to specify the matched address (which complies with the URL mode defined in the registered address template ). As shown in, because the address (http ://... /home/index) corresponds to the index operation of homecontroller, so the corresponding method is executed, and the execution result is to display the name of the target contrller and action of the current request.
The above demonstrates how to create a "mini version" ASP. create a web application in the. net mvc framework. net MVC applications. Next, we will gradually analyze how the custom ASP. net mvc framework is built, and it also represents the working principle of the real ASP. net mvc framework.
How ASP. net mvc runs [1]: Web applications built on the "pseudo" MVC Framework
How ASP. net mvc runs [2]: URL Routing
How ASP. net mvc runs [3]: controller Activation
How ASP. net mvc runs [4]: execution of action