Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the execution process of ASP.NET MVC Application

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

本篇文章给大家分享的是有关如何进行ASP.NET MVC应用程序执行过程的分析,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创建一个简单的ASP.NET MVC应用程序

ASP.NET MVC框架提供了支持Visual Studio的工程模板,从而让你创建支持MVC模式的Web应用程序。

这些MVC工程模板包括:

"ASP.NET MVC Web Application"模板

"ASP.NET MVC Web Application and Test"模板

这些模板可以用于创建一个新的基于ASP.NET MVC框架的Web应用程序。在这些程序中,你可以使用文件夹,模板以及配置文件等手段配置它们。

默认情况下,当你使用"ASP.NET MVC Web Application and Test"模板创建一个新的Web应用程序时,Visual Studio将创建一个添加有两个工程的方案。***个工程是一个Web工程,你可以在其中实现你的应用程序。第二个工程是一个测试工程,你可以在此为你的MVC组件编写单元测试。

【注意】"ASP.NET MVC Web Application"模板基于"ASP.NET Web Application"模板。所以,在创建基于ASP.NET MVC框架的网站时,你需要从"File"菜单下选择"New Project",然后选择一个新的ASP.NET MVC工程,而不是选择"New Website"。

***,你可以使用任何与.NET框架兼容的单元测试框架来测试ASP.NET MVC应用程序。注意,Visual Studio 2008 Professional(以及Team System)已经提供了对于MSTest测试工程的内置的支持。

Web应用程序的MVC工程架构

当你创建一个ASP.NET MVC应用程序工程时,诸MVC组件将基于如图1所示的工程文件夹加以分离:

图1-一个ASP.NET MVC应用程序的典型架构

Views文件夹。该Views文件夹是推荐的放置你的视图的位置。视图组件主要使用.aspx,.ascx和.master文件实现;此外,也有可能使用其他任何与视图有关联的文件。Views文件夹下针对每一个控制器都提供一个独立的文件夹,而且此文件夹以该控制器的名字为前缀命名。例如,如果你有一个名字为HomeController的控制器,那么你的Views文件夹下应该包含一个名字为Home的文件夹。默认情况下,当ASP.NET MVC框架加载一个视图时,它将首先在Views\controllerName文件夹下使用要求的视图名查找一个相应的.aspx文件。此外,还存在一个缺省名字为Common的文件夹,不过它没有对应任何控制器。你可以在这个位置放置母版页面,脚本,CSS文件,以及其它当生成视图时所使用的文件。

Controllers文件夹。Controllers文件夹是推荐的放置控制器的位置。

Models文件夹。该Models文件夹是推荐的放置你的MVC Web应用程序的模型的位置。典型情况下,这里会包括定义与数据存储进行交互的逻辑代码,还有对象定义等等。

App_Data。App_Data文件夹对应存储数据的物理位置。这个文件夹与在ASP.NET Web应用程序中的角色一样。

除了上面列举的文件夹外,一个MVC Web应用程序还使用下列一些重要的应用程序元素:

Global.asax和Global.asax.cs。在文件Global.asax.cs的Application_Start方法中实现对路由的初始化。下面的代码展示了一个典型的Global.asax文件,其中包括了默认的路由逻辑。

public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { //注意:如果把下列表达改写成Url="{controller}.mvc/{action}/{id}"即可自动支持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() }); } }

配置文件。MVC Web应用程序配置文件Web.config负责注册HTTP模块。在httpModules节中实现注册UrlRoutingModule类,这个类负责分析URL并且把请求路由到适当的处理器。注意,这个入口能够支持应用程序在同一个工程中宿主MVC与非MVC处理器。

下列代码展示了一个ASP.NET MVC应用程序的httpModules节的内容:

< 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 2008 Professional (or Team System), a test project is automatically included in the scenario. You can use MVC templates to create mock implementations of tests and built-in interfaces.

Understand the execution process of MVC projects

A Web application request sent to ASP.NET MVC is first passed to the UrlRoutingModule object (which is an HTTP module). The UrlRoutingModule object then parses the request and performs routing. Notably, UrlRoutingModule will select *** Route objects that match the current request.

Next, the UrlRoutingModule object fetches the IHttpContext object from the selected routing object--which processes the request further. By default, this IHttpContext is the MvcHandler object. The MvcHandler object then further selects the appropriate controller, which ultimately handles the request.

Note: When an ASP.NET MVC Web application runs in IIS7, it is not required to specify file extensions for MVC projects. However, in IIS6, the processor requires you to map the.mvc file extension to ASP.NET ISAPI.

Modules and processors become portals to the ASP.NET MVC framework and perform the following actions:

Select the appropriate controller in an MVC Web application

◆ Get a specific controller instance

◆ Call the Execute method of the controller

Table 1 describes in more detail the various execution phases of an MVC Web project.

Table 1-Implementation Phases of an MVC 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 from matching Route objects in RouteTable instances. This routing data is used to determine the controller requested and the behavior to invoke.

Map to controller

The MvcRouteHandler handler is responsible for creating controller type names from data in RouteData instances.

Call controller builder

The handler calls the global static CreateController method of the ControllerBuilder class to get an instance of IController. If an IController instance is not returned, the handler returns an HTTP 500 error indicating that a server error occurred.

creation controller

Create a new controller directly from the ControllerBuilder instance, or use an IControllerFactory object to create the controller.

execution controller

The MvcHandler instance is added to the ControllerContext object and calls the Execute method of that controller.

We analyze Microsoft's MVC (Model-View-Controller) pattern theoretically and compare it with traditional ASP.NET Web forms patterns and page postback schemes. After understanding the main functions of the MVC framework components, *** we made a rough summary of the various execution stages of a typical MVC Web project.

It should be noted that the ASP.NET MVC pattern is a development pattern launched by Microsoft, and the current beta version is Preview 2 (Preview 3 is also coming soon). Therefore, whether this pattern is more conducive to improving the productivity of software than ASP.NET Web Forms pattern remains to be tested by development practice.

The above is how to analyze the execution process of ASP.NET MVC application, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report