ASP. net mvc application Execution Process Analysis

Source: Internet
Author: User

Create a simple ASP. net mvc application

The ASP. net mvc Framework provides engineering templates that support Visual Studio, allowing you to create Web applications that support the MVC mode.

These MVC engineering Templates include:

"ASP. net mvc Web Application" template

"ASP. net mvc Web Application and Test" template

These templates can be used to create a new Web Application Based on the ASP. net mvc framework. In these programs, you can use folders, templates, configuration files, and other methods to configure them.

By default, when you use the "ASP. net mvc Web Application and Test" template to create a new Web Application, Visual Studio will create a solution to add 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 components.

Note: The "ASP. net mvc Web Application" template is based on the "ASP. NET Web Application" template. Therefore. net mvc framework, you need to select "New Project" from the "File" menu, and then select a New ASP.. net mvc project, instead of selecting "New Website ".

Finally, you can use any unit test framework compatible with the. NET Framework to test ASP. net mvc applications. Note that Visual Studio 2008 Professional and Team System have provided built-in support for the MSTest test project.

MVC engineering architecture of Web Applications

When you create an ASP. net mvc application project, the MVC components are separated based on the project folder shown in 1:

Figure 1-typical architecture of an ASP. net mvc application

Views folder. The Views folder is recommended for placing your Views. The view component mainly uses the. aspx,. ascx, And. master files. In addition, it may also use any other files associated with the view. The Views folder provides an independent folder for each controller, and this folder is named with the name of this controller as the prefix. For example, if you have a controller named HomeController, your Views folder should contain a folder named Home. By default, when the ASP. net mvc Framework loads a view, it first searches for a corresponding. aspx file using the required view name in the Views \ controllerName folder. In addition, there is a folder named "Common" by default, but it does not correspond to any controller. You can place master pages, scripts, CSS files, and other files used to generate views at this location.

The Controllers folder. The Controllers folder is the recommended location for storing Controllers.

Models folder. The Models folder is recommended for placing the model of your MVC Web application. In typical cases, logic Code defining interaction with data storage and object definition are included.

App_Data. The App_Data folder corresponds to the physical location of the stored data. This folder is the same as the role in an ASP. NET Web application.

In addition to the folders listed above, an MVC Web application also uses the following important application elements:

Global. asax and Global. asax. cs. Initialize the route in the Application_Start method of the file Global. asax. cs. The following code shows a typical Global. asax file, including the default routing logic.

 
 
  1. Public ClassGlobal: System. Web. HttpApplication
  2. {
  3. Protected VoidApplication_Start (ObjectSender, EventArgs e)
  4. {
  5. // Note: If you rewrite the following expression to Url = "{controller}. mvc/{action}/{id}", IIS6 is automatically supported. 
  6. RouteTable. Routes. Add (NewRoute
  7. {
  8. Url ="{Controller}/{action}/{id }",
  9. Defaults =New{Action ="Index", Id = (String)Null},
  10. RouteHandler =NewMvcRouteHandler ()
  11. });
  12. RouteTable. Routes. Add (NewRoute
  13. {
  14. Url ="Default. aspx",
  15. Defaults =New{Controller ="Home", Action ="Index", Id = (String)Null},
  16. RouteHandler =NewMvcRouteHandler ()
  17. });
  18. }
  19. }

Configuration file. The MVC Web application configuration file Web. config is responsible for registering the HTTP module. Register the UrlRoutingModule class in the httpModules Section. This class analyzes the URL and routes the request to the appropriate processor. Note that this entry supports hosting MVC and non-MVC processors for applications in the same project.

The following code shows the content of the httpModules section of an ASP. net mvc application:

 
 
  1. < httpModules> 
  2.  < add name="UrlRoutingModule" 
  3.    type="System.Web.Mvc.UrlRoutingModule,  
  4.    System.Web.Extensions, Version=3.6.0.0, Culture=neutral,  
  5.    PublicKeyToken=31BF3856AD364E35" /> 
  6. < /httpModules> 

When you select an ASP. net mvc Web application and test project template in Visual Studio 2008 Professional or Team System, a test project is automatically included in the solution. You can use the MVC template to create a mock implementation for testing and internal interfaces.

Understand the execution process of the MVC Project

A Web application request sent to ASP. net mvc must first be passed to the UrlRoutingModule object. This is an HTTP module ). Then, the UrlRoutingModule Object Analyzes the request and executes route selection. It is worth noting that the UrlRoutingModule selects the first Route object that matches the current request.

Next, the UrlRoutingModule object obtains the IHttpContext object from the selected route object and processes the request further. By default, this IHttpContext is the MvcHandler object. Then, the MvcHandler object selects the corresponding controller and then the controller processes the request.

Note: When an ASP. net mvc Web application runs in the IIS7 environment, you do not need to specify the file extension for the MVC project. However, in IIS6, the processor requires you to map the. mvc file extension to ASP. net isapi.

The module and processor become the entry to the ASP. net mvc Framework and perform the following actions:

◆ Select an appropriate controller in an MVC Web Application

◆ Obtain a specific controller instance

◆ Call the Execute method of the Controller

Table 1 details the various stages of an MVC Web project.

Table 1-implementation phases of the MVC Web Project

Phase

Detailed description

Initial Request

InGlobal. asaxFile, add the routeRouteTableObject.

Routing

UrlRoutingModuleModuleRouteTableTheRouteObject CreationRouteDataObject. These routing data are used to determine the requested controller and the behavior to call.

Map to Controller

MvcRouteHandlerProcessor is responsibleRouteDataThe name of the Data creation controller type in the instance.

Call the Controller build ProgramControllerbuilder)

Processor callControllerBuilderGlobal staticCreateControllerMethod to obtainIControllerInstance. If noIControllerInstance, the processor returnsHTTP 500Indicates a server error.

Create a controller

ByControllerBuilderThe instance directly creates a new controller or usesIControllerFactoryObject To create the controller.

Execution Controller

MvcHandlerInstance addedControllerContextObject and callExecuteMethod.

 

Summary

In this article, we conduct a theoretical analysis of the latest MVC Model-View-Controller) model launched by Microsoft, and compare it with the traditional ASP. NET Web form mode and page return solution are compared. After learning about the main functions of the MVC framework components, we finally roughly summarize the various stages of a typical MVC Web project.

Note that ASP. net mvc is the latest development mode released by Microsoft. The latest version Preview 2 Preview 3 is coming soon ). Therefore, whether this mode is more conducive to improving software production efficiency than ASP. NET Web form mode remains to be tested by development practices.

This article is from Zhu Xianzhong's blog: in-depth analysis of ASP. NET 3.5 MVC framework.

  1. Some basic knowledge about ASP. net mvc Framework
  2. Scott Gu teaches you how to use ASP. net mvc 2 new features
  3. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  4. 13 uncertain ASP. net mvc extensions
  5. Learning the basic concepts of ASP. NET MVC

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.