How asp.net MVC works [1]: Web applications built on the "pseudo" MVC framework

Source: Internet
Author: User
Tags definition visual studio

Asp. NET because of adopting the pipeline design, has the very good expansibility, but the entire ASP.net MVC application framework is realizes through the extension asp.net. With the introduction of the ASP.net piping design above, we know that asp.net extension points as long as they are embodied in the Httpmoudle and HttpHandler these two core builds, in fact the entire asp.net The MVC framework is implemented through custom Httpmoudle (UrlRoutingModule) and HttpHandler (Mvchandler). In order to get the reader to grasp the working mechanism of asp.net MVC as a whole, I follow the principle of using some custom components to simulate the operation of ASP.net mvc, we can also consider this as a "mini version" of 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 this instance, I have completely adopted the type naming method consistent with ASP.net mvc.

Before introducing the implementation of the mini version of ASP.net mvc we created ourselves, let's take a look at how Web applications built on the framework are implemented. We create an empty ASP.net Web application through Visual Studio (note that it is not asp.net mvc application), We do not reference System.Web.Mvc.dll this assembly, so the type of the same name that you see in the next program is defined in the assembly.

We first define the following Simplemodel type, which indicates the final need to bind to the data on the view. For simplicity, and in order to verify the resolution mechanism for Controller and action, the two properties defined by Simplemodel represent 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 real ASP. NET MVC application Development, we need to define the controller type. As shown in the following code snippet, we define a homecontroller as we know the naming method (with the character controller as the suffix). The abstract type Controllerbase implemented by HomeController is our own definition. The index method with a custom ActionResult as the return type represents the controller action, which accepts an object of Simplemodel type as an argument. The ActionResult returned by the action method is a Rawcontentresult object, as the name suggests, Rawcontentresult is the specified content to be displayed as is. Here we will display the Controller and action attributes of the Simplemodel object as a parameter.

   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 to new Rawcontentresult (content);
7: }
8:}

asp.net mvc resolves the type of controller used to process the request and the name of the action method based on the request address. Specifically, we register some (relative) address templates that contain controller and action names as position characters, and Controller and action names can be parsed correctly if the request address conforms to the pattern of the corresponding address template. Similar to the ASP.net MVC application, we 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 shown above for address template registration, we also registered a factory for creating controller objects. The previously defined HomeController definition, under Namespace WebApp, requires that the namespace be registered as the default namespace for the current Controllerbuilder because only the name of the controller type can be extracted from the request address. RouteTable, Controllerbuilder, and defaultcontrollerfactory are all our custom types.

As I said above, asp.net MVC is implemented through a custom HttpModule, which we also name as UrlRoutingModule in this "mini" asp.net MVC framework. Before running the Web application, we need to configure the custom HttpModule to register, and here is 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 the programming and configuration work has been done. In order for the action Method index defined in HomeController to handle the access request for the Web application, we need to specify the address to match (the URL pattern defined in the registered address template). As the following illustration shows, the corresponding method is executed because the input address (http://.../Home/Index) in the browser corresponds exactly to the HomeController Index operation. The result of the execution is to display the name of the target Contrller and action for the current request.

Above we demonstrated how to create a Web application in our own mini asp.net MVC framework, from which we can see and create a true asp.net MVC application identical. Let's go through the step-by-step analysis of how this custom asp.net MVC framework is built, and it also represents the workings of the true asp.net MVC framework.

Supporting Source: http://www.bianceng.net/dotnet/201210/533.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.