ASP. Application execution Process Analysis 2009-08-14 17:57 Zhu Xianzhong Zhu Xianzhong Blog size: T | T
The ASP. NET MVC Framework provides a project template that supports Visual Studio. This article describes the execution process analysis of an ASP. NET MVC application.
AD:WOT2015 Internet operations and developers Conference selling tickets
Create a simple ASP. NET MVC Application
The ASP. NET MVC Framework provides a project template that supports Visual Studio, allowing you to create Web applications that support MVC patterns.
These MVC project templates include:
The ASP. NET MVC Web Application Template
"ASP. NET MVC Web application and Test" template
These templates can be used to create a new ASP. NET MVC framework-based Web application. In these programs, you can configure them by using folders, templates, and configuration files.
By default, when you create a new WEB application using the ASP. NET MVC Web application and Test template, Visual Studio creates a scenario that adds two projects. The first project is a Web project where you can implement your application. The second project is a test project where you can write unit tests for your MVC component.
Note the ASP. NET MVC Web Application template is based on the ASP. NET Web Application template. So, when you create a Web site based on the ASP. NET MVC framework, you need to choose New Project from the File menu, and then choose a newer ASP project instead of "New Website".
Finally, you can use any with. NET Framework-compatible unit testing framework to test ASP. Note that Visual Studio Professional (and Team System) already provides built-in support for MSTest test engineering.
MVC Engineering Architecture for Web applications
When you create an ASP. NET MVC Application project, the various MVC components are separated based on the project folder shown in 1:
Figure 1-Typical architecture for an ASP. NET MVC Application
Views folder. The Views folder is the recommended location for placing your view. View components are primarily implemented using. aspx,.ascx and. master files, and it is possible to use any other file that is associated with the view. The Views folder provides a separate folder for each controller, and this folder is named after the name of the controller. For example, if you have a controller named HomeController, then your Views folder should contain a folder named home. By default, when the ASP. NET MVC framework loads a view, it first finds a corresponding. aspx file under the Views\controllername folder using the required view name. In addition, there is a default name of common folder, but it does not correspond to any controller. You can place master pages, scripts, CSS files in this location, and other files that are used when the view is generated.
Controllers folder. The Controllers folder is the recommended location for placing the controller.
Models folder. The Models folder is the recommended location for placing the model of your MVC Web application. Typically, this includes defining logical code that interacts with the data store, as well as object definitions, and so on.
App_Data. The App_Data folder corresponds to the physical location where the data is stored. This folder is the same as the role in the ASP. NET Web application.
In addition to the folders listed above, an MVC Web application uses some of the following important application elements:
Global.asax and Global.asax.cs. The initialization of the route is implemented in the Application_Start method of the file Global.asax.cs. The following code shows a typical Global.asax file that includes the default routing logic.
- Public class Global:System.Web.HttpApplication
- {
- protected void Application_Start (object sender, EventArgs e)
- {
- Note: If you rewrite the following expression to Url= "{Controller}.mvc/{action}/{id}", you can automatically support IIS6
- ROUTETABLE.ROUTES.ADD (New Route
- {
- Url = "{Controller}/{action}/{id}",
- Defaults = new {action = "Index", id = (string) null},
- Routehandler = new Mvcroutehandler ()
- });
- ROUTETABLE.ROUTES.ADD (New Route
- {
- URL = "default.aspx",
- Defaults = new {controller = "Home", action = "Index", id = (string) null},
- Routehandler = new Mvcroutehandler ()
- });
- }
- }
Configuration file. The MVC Web application configuration file Web. config is responsible for registering the HTTP module. Implement the Register UrlRoutingModule class in the httpmodules section, which is responsible for parsing the URL and routing the request to the appropriate processor. Note that this portal enables applications to host MVC and non-MVC processors in the same project.
The following code shows the contents of the httpmodules section of an ASP. NET MVC application:
- < httpmodules>
- < add Name= "UrlRoutingModule"
- Type= "System.Web.Mvc.UrlRoutingModule,
- System.Web.Extensions, version=3.6.0.0, Culture=neutral,
- Publickeytoken=31bf3856ad364e35 " />
- < /httpmodules>
When you select an ASP. NET MVC Web application and test project template in Visual Studio Professional (or Team System), a test project is automatically included in the scenario. You can use the MVC template to create tests and mock implementations of the intrinsic interfaces.
Understand the implementation process of MVC Project
A Web application request sent to ASP. NET MVC is first passed to the UrlRoutingModule object (this is an HTTP module). The UrlRoutingModule object then parses the request and executes the route selection. It is worth noting that UrlRoutingModule will select the first route object that matches the current request.
Next, the UrlRoutingModule object gets the Ihttpcontext object from the selected routing object-it further processes the request. By default, this ihttpcontext is the Mvchandler object. The Mvchandler object then further selects the appropriate controller, which is eventually processed by the controller to handle the request.
"Note" When an ASP. NET MVC Web application runs in a IIS7 environment, it does not require the extension of the file to be specified for the MVC project. However, in IIS6, the processor requires that you map the. mvc file extension to the ASP. NET ISAPI.
Modules and processors become portals to the ASP. NET MVC framework and perform the following actions:
Selecting the appropriate controller in an MVC Web application
To get a specific controller instance
Call the Execute method of the controller
Table 1 describes in more detail the various stages of the implementation of an MVC Web project.
Table 1-mvc Each execution phase of a Web project
Stage |
Detailed description |
Initial request |
In the Global.asax file, add the route to the RouteTable object. |
Routing |
The UrlRoutingModule module creates Routedata objects based on the matching route objects in the RouteTable instance. These routing data are used to determine the requested controller and the behavior to invoke. |
Map to Controller |
The Mvcroutehandler processor is responsible for creating the controller type name based on the data in the Routedata instance. |
Calling the Controller builder (Controllerbuilder) |
The processor calls the global static Createcontroller method of the Controllerbuilder class to obtain a IController instance. If a IController instance is not returned, the processor will return an HTTP 500 error indicating that a server error has occurred. |
Create a Controller |
Create a new controller directly from the Controllerbuilder instance, or use a Icontrollerfactory object to create the controller. |
Execution controller |
The Mvchandler instance is added to the ControllerContext object and the Execute method of the controller is called. |
Summary
In this article, we have a theoretical analysis of the latest MVC (model-View-Controller) model of Microsoft, and compare it with the traditional ASP. NET Web Form mode and page return scheme. After understanding the main functions of each component of the MVC framework, we conclude a rough summary of the various execution phases of a typical MVC Web project.
Finally, it is important to note that the ASP. NET MVC pattern is Microsoft's latest development model, the latest beta version is preview 2 (Preview 3 is also coming soon). Therefore, whether this model is more conducive to improving the productivity of software than the ASP. NET Web Form mode is still to be tested in development practice.
This article is from Zhu Xianzhong's blog: "ASP. NET 3.5 MVC Framework Depth Resolution".
ASP. NET MVC application Execution Process analysis