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 code download http://www.bkjia.com/uploadfile/2012/0517/20120517102538221.rar 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 applications (not ASP.. net mvc application), we will not reference System. web. mvc. dll assembly, so the type of the same name 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 class SimpleModel
2:
{
3: public string Controller {get; set;
}
4: public string Action {get; set;
}
5:
}
Like ASP. net mvc application development, we need to define the Controller type. As shown in the following code snippet, according to the naming method we are familiar with (using 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 class HomeController: ControllerBase
2:
{
3: public ActionResult Index (SimpleModel model)
4:
{
5: string content = string. format ("Controller: {0} <br/> Action: {1}", model. controller, model. action); 6: return new RawContentResult (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 class Global: System. Web. HttpApplication
2:
{
3: protected void Application_Start (object sender, EventArgs e)
4 :{
5: RouteTable. Routes. Add ("default", new Route {Url = "{controller}/{action }"});
6: ControllerBuilder. Current. SetControllerFactory (new DefaultControllerFactory ());
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.
From Jiang Jinnan's blog