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 框架包括以下组件:

  

  Figure 01: Invoking a controller action that expects a parameter value(Click to view full-size image)

  • Models. Model objects are the parts of the application that implement the logic for the application s data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in SQL Server.

  图 01: 调用一个期望(单击以查看完整大小的图片)的参数值的控制器动作

  • 模型模型对象是实现应用程序的数据域的逻辑的应用程序部件。通常情况下,模型对象检索,并将模型状态存储在数据库中。例如,一个产品对象可能会从数据库中检索信息操作上它,,然后将更新后的信息写回到在 SQL Server 中的产品表

  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.

  • Views. Views are the components that display the application s user interface (UI). Typically, this UI is created from the model data. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Products object.

  • Controllers. Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction. For example, the controller handles query-string values, and passes these values to the model, which in turn queries the database by using the values.

  在小型应用程序,该模型往往是概念而不是一种物理分离。例如,如果应用程序只读取一个数据集,并将其发送到视图,应用程序没有物理模型层和关联的类。在这种情况下,数据集对模型对象的角色。

  • 的意见视图是显示应用程序的用户界面 (UI) 的组件。通常,此 UI 被创建的模型数据。示例将显示文本框、 下拉列表和复选框基于一个产品对象的当前状态的产品表的编辑视图。

  • 控制器控制器是用于处理用户交互、 模型,并最终选择要呈现的视图来显示 UI 的组件。在 MVC 应用程序中,视图仅显示信息;控制器处理和响应用户输入和交互。例如,控制器处理查询字符串值,并将这些值传递给模型,反过来查询数据库所使用的值。

  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 应用程序的三个主要组件之间的松散耦合也可促进并行开发。例如,一个开发者可以工作在视图上,第二个开发人员可以在控制器逻辑,工作和第三个开发人员可以专注于业务逻辑模型中。

Deciding When to Create an MVC Application

决定何时创建 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 站点之前,请权衡每种方法的优点。

Advantages of an MVC-Based Web Application

The ASP.NET MVC framework offers the following advantages:

基于 MVC 的 Web 应用程序的优点

ASP.NET MVC 框架具有以下优点:

  • It makes it easier to manage complexity by dividing an application into the model, the view, and the controller.
  • It does not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application.
  • It uses a Front Controller pattern that processes Web application requests through a single controller. This enables you to design an application that supports a rich routing infrastructure. For more information, seeFront Controlleron the MSDN Web site.
  • It provides better support for test-driven development (TDD).
  • It works well for Web applications that are supported by large teams of developers and Web designers who need a high degree of control over the application behavior.
  • 它很容易通过将应用程序划分为模型、 视图和控制器管理的复杂性。
  • 它不使用视图状态或基于服务器的窗体。这使得 MVC 框架特别适合开发人员想要完全控制应用程序的行为。
  • 它使用处理 Web 应用程序请求通过单一控制器前端控制器模式。这使您能够设计支持丰富的路由基础结构的应用程序。更多的信息,请参阅 MSDN 网站上的前端控制器
  • 它为测试驱动开发 (TDD) 提供了更好的支持。
  • 它非常适合 Web 应用程序支持的大型团队的开发人员和 Web 设计人员需要高度的控制应用程序的行为。

Advantages of a Web Forms-Based Web Application

The Web Forms-based framework offers the following advantages:

Web 窗体基于 Web 应用程序的优点

基于 Web 窗体框架具有以下优点:

  • It supports an event model that preserves state over HTTP, which benefits line-of-business Web application development. The Web Forms-based application provides dozens of events that are supported in hundreds of server controls.
  • It uses a Page Controller pattern that adds functionality to individual pages. For more information, seePage Controlleron the MSDN Web site.
  • It uses view state or server-based forms, which can make managing state information easier.
  • It works well for small teams of Web developers and designers who want to take advantage of the large number of components available for rapid application development.
  • In general, it is less complex for application development, because the components (thePageclass, controls, and so on) are tightly integrated and usually require less code than the MVC model.
  • 它支持通过 HTTP,有利于业务线 Web 应用程序开发保留状态的事件模型。基于 Web 窗体的应用程序提供了许多支持数百个服务器控件的事件。
  • 它使用一种页面控制器模式,将功能添加到单个页面。更多的信息,请参阅 MSDN 网站上的页面控制器
  • 它使用视图状态或基于服务器的窗体,可以使管理状态信息更容易。
  • 它非常适合小团队的 Web 开发人员和设计师们想要利用大量的可用快速开发应用程序的组件。
  • 一般情况下,它是应用程序开发的复杂程度较低,因为组件 (Page类、 控件和等等) 紧密集成,并且通常需要更少的代码比 MVC 模型。

Features of the ASP.NET MVC Framework

The ASP.NET MVC framework provides the following features:

ASP.NET MVC 框架的功能

ASP.NET MVC 框架提供了以下功能:

  • Separation of application tasks (input logic, business logic, and UI logic), testability, and test-driven development (TDD) by default. All core contracts in the MVC framework are interface-based and can be tested by using mock objects, which are simulated objects that imitate the behavior of actual objects in the application. You can unit-test the application without having to run the controllers in an ASP.NET process, which makes unit testing fast and flexible. You can use any unit-testing framework that is compatible with the .NET Framework.
  • An extensible and pluggable framework. The components of the ASP.NET MVC framework are designed so that they can be easily replaced or customized. You can plug in your own view engine, URL routing policy, action-method parameter serialization, and other components. The ASP.NET MVC framework also supports the use of Dependency Injection (DI) and Inversion of Control (IOC) container models. DI allows you to inject objects into a class, instead of relying on the class to create the object itself. IOC specifies that if an object requires another object, the first objects should get the second object from an outside source such as a configuration file. This makes testing easier.
  • A powerful URL-mapping component that lets you build applications that have comprehensible and searchable URLs. URLs do not have to include file-name extensions, and are designed to support URL naming patterns that work well for search engine optimization (SEO) and representational state transfer (REST) addressing.
  • Support for using the markup in existing ASP.NET page (.aspx files), user control (.ascx files), and master page (.master files) markup files as view templates. You can use existing ASP.NET features with the ASP.NET MVC framework, such as nested master pages, in-line expressions (<%= %>), declarative server controls, templates, data-binding, localization, and so on.
  • Support for existing ASP.NET features. ASP.NET MVC lets you use features such as forms authentication and Windows authentication, URL authorization, membership and roles, output and data caching, session and profile state management, health monitoring, the configuration system, and the provider architecture.
  • 分离的应用程序任务 (输入的逻辑、 业务逻辑和 UI 逻辑)、 可测试性和测试驱动开发 (TDD) 在默认情况下。在 MVC 框架中的所有核心合同是基于接口的可以测试通过使用 mock 对象,是模仿实际应用程序中的对象的行为的模拟的对象。你可以单元测试应用程序而无需运行控制器在 ASP.NET 进程,这使得单元测试既快速又灵活。你可以使用任何与.NET 框架兼容的单元测试框架。
  • 可扩展和可插入的框架。ASP.NET MVC 框架的组件设计以便他们可以方便地更换或自定义。您可以插入自己的视图引擎、 URL 路由策略、 操作方法参数序列化以及其他组件。ASP.NET MVC 框架还支持使用依赖注入 (DI) 和控制反转 (IOC) 容器模型。DI 允许您对象注入一个类,而不是依靠类来创建对象本身。IOC 指定某个对象是否需要另一个对象,第一个对象应该从配置文件之类的外部源中获取第二个对象。这使得测试更容易。
  • 强大的 URL 映射组件,允许您构建的应用程序具有可理解性和可搜索的 Url。Url 不必须包括文件扩展名,并旨在支持 URL 命名模式,工作的很好的搜索引擎搜索引擎优化 (SEO) 和具象状态传输 (REST) 处理。
  • 使用现有 ASP.NET 网页 (.aspx 文件),用户控件 (.ascx 文件) 和主页面 (.master 文件) 标记文件中的标记作为视图模板的支持。你可以使用现有的 ASP.NET 功能与 ASP.NET MVC 框架中,如嵌套母版页,线在表达式 (< %= %>),声明服务器控件、 模板、 数据绑定、 本地化和等等。
  • 对现有的 ASP.NET 功能的支持。ASP.NET MVC 允许您使用窗体身份验证和 Windows 身份验证、 URL 授权、 会员和角色、 输出和数据缓存、 会话和配置文件的状态管理、 健康监测、 配置系统和提供程序体系结构的功能。

优质内容筛选与推荐>>
1、I2C总线学习(三)--寻址
2、关于Apache多个工程域名的配置
3、51CTO大赛,欢迎投博主一票
4、利用工作流返回达到无限次重复办理业务的过程
5、元宵每斤普涨近两元 原因:原材料价格在上涨


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn