ASP.NET MVC Overview
Learn about the differences between ASP.NET MVC application and ASP.NET Web Forms applications. Learn how to decide when to build an ASP.NET MVC application.
了解 ASP.NET MVC 应用程序和 ASP.NET Web 窗体应用程序之间的差异。了解如何决定何时生成 ASP.NET MVC 应用程序。
The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in theSystem.Web.Mvcnamespace and is a fundamental, supported part of theSystem.Webnamespace.
模型-视图-控制器 (MVC) 体系结构模式将应用程序分成三个主要组件: 模型、 视图和控制器。ASP.NET MVC 框架提供了用于创建基于 MVC 的 Web 应用程序的 ASP.NET Web 窗体模式的替代方法。ASP.NET MVC 框架是一种重量轻、 高度可测试演示框架,(与基于 Web 窗体的应用程序一样) 结合现有的 ASP.NET 功能,例如母版页和基于会员的身份验证。MVC 框架在System.Web.Mvc命名空间中定义,并是基本、 支持System.Web命名空间的一部分。
MVC is a standard design pattern that many developers are familiar with. Some types of Web applications will benefit from the MVC framework. Others will continue to use the traditional ASP.NET application pattern that is based on Web Forms and postbacks. Other types of Web applications will combine the two approaches; neither approach excludes the other.
MVC 是许多开发人员熟悉的标准设计模式。某些类型的 Web 应用程序将受益于 MVC 框架。其他人将继续使用传统的 ASP.NET 应用程序模式,基于 Web 窗体和回发。其他类型的 Web 应用程序将结合这两种方法;两种方法排除其他。
The MVC framework includes the following components:
MVC 框架包括以下组件:
In small applications, the model is often a conceptual separation instead of a physical one. For example, if the application only reads a data set and sends it to the view, the application does not have a physical model layer and associated classes. In that case, the data set takes on the role of a model object.
在小型应用程序,该模型往往是概念而不是一种物理分离。例如,如果应用程序只读取一个数据集,并将其发送到视图,应用程序没有物理模型层和关联的类。在这种情况下,数据集对模型对象的角色。
The MVC pattern helps you create applications that separate the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The pattern specifies where each kind of logic should be located in the application. The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps you manage complexity when you build an application, because it enables you to focus on one aspect of the implementation at a time. For example, you can focus on the view without depending on the business logic.
MVC 模式可以帮助您创建独立应用程序 (输入的逻辑、 业务逻辑和 UI 逻辑),同时提供这些元素之间的松散耦合的不同方面的应用。该模式指定每种逻辑应被放置在应用程序中的何处。用户界面逻辑属于在视图中。输入的逻辑属于控制器。业务逻辑是属于模型。这种分离可以帮助您管理的复杂性,当您生成应用程序,因为它使您能够集中在一次执行的一个方面。例如,你可以专注于根据业务逻辑没有视图。
In addition to managing complexity, the MVC pattern makes it easier to test applications than it is to test a Web Forms-based ASP.NET Web application. For example, in a Web Forms-based ASP.NET Web application, a single class is used both to display output and to respond to user input. Writing automated tests for Web Forms-based ASP.NET applications can be complex, because to test an individual page, you must instantiate the page class, all its child controls, and additional dependent classes in the application. Because so many classes are instantiated to run the page, it can be hard to write tests that focus exclusively on individual parts of the application. Tests for Web Forms-based ASP.NET applications can therefore be more difficult to implement than tests in an MVC application. Moreover, tests in a Web Forms-based ASP.NET application require a Web server. The MVC framework decouples the components and makes heavy use of interfaces, which makes it possible to test individual components in isolation from the rest of the framework.
除了管理复杂性,MVC 模式使它容易测试应用程序,而不是测试一个 Web 窗体基于 ASP.NET Web 应用程序。例如,在 Web 窗体基于 ASP.NET Web 应用程序中,一个单独的类用来显示输出,对用户输入作出响应。编写自动化的测试的 Web 窗体基于 ASP.NET 应用程序可以是复杂,因为要测试单个页面,您必须实例化的页类,所有子控件和应用程序中的其他相关类。因为这么多的类的实例化运行页,它可以很难编写专门侧重于应用程序的各个部分的测试。因此,基于 Web 窗体的 ASP.NET 应用程序的测试可以更难实现比在 MVC 应用程序中的测试。此外,基于 Web 窗体的 ASP.NET 应用程序中测试需要 Web 服务器。MVC 框架可使组件,并大量使用接口,这使得它能够从框架的其余部分单独测试单个组件。
The loose coupling between the three main components of an MVC application also promotes parallel development. For instance, one developer can work on the view, a second developer can work on the controller logic, and a third developer can focus on the business logic in the model.
MVC 应用程序的三个主要组件之间的松散耦合也可促进并行开发。例如,一个开发者可以工作在视图上,第二个开发人员可以在控制器逻辑,工作和第三个开发人员可以专注于业务逻辑模型中。
You must consider carefully whether to implement a Web application by using either the ASP.NET MVC framework or the ASP.NET Web Forms model. The MVC framework does not replace the Web Forms model; you can use either framework for Web applications. (If you have existing Web Forms-based applications, these continue to work exactly as they always have.)
你必须仔细考虑是否以实现 Web 应用程序通过使用 ASP.NET MVC 框架或 ASP.NET Web 窗体模型。MVC 框架不能代替 Web 窗体模型;对于 Web 应用程序,您可以使用任何一种框架。(如果您有现有的基于 Web 窗体应用程序,这些继续工作完全是因为他们总是有)。
Before you decide to use the MVC framework or the Web Forms model for a specific Web site, weigh the advantages of each approach.
您决定使用 MVC 框架或 Web 窗体模型,为特定的 Web 站点之前,请权衡每种方法的优点。
The ASP.NET MVC framework offers the following advantages:
ASP.NET MVC 框架具有以下优点:
The Web Forms-based framework offers the following advantages:
基于 Web 窗体框架具有以下优点:
The ASP.NET MVC framework provides the following features:
ASP.NET MVC 框架提供了以下功能: