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 the System.Web.Mvc namespace and is a fundamental, supported part of the System.Web namespace.

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架构模式将应用程序分为模型-视图-控制器三个主要组织部分,ASP.NET MVC框架提供一种可以代替ASP.NET Web Forms模式来创建基于MVC的应用程序,ASP.NET MVC是轻量级的,高可测试的框架,并且可以结合现有的ASP.NET的特性如母板、基于membership的身份验证,MVC框架基础定义在System.Web.Mvc命名空间中的,成为System.Web命名空间的一部分。

MVC作为一个标准的设计模式被广大的开发人员所熟知。某些类型的Web应用程序将受益于MVC framework。其它类型的应用程序可以继续使用传统的基于Postbacks的Web forms模式进行ASP.NET开发,也可以结合这两种模式,它们是并行存在而不是互相冲突的。

The MVC framework includes the following components:

MVC框架包含如下组成部分:

clip_image001

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.

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.

模型:模型对象是【实现应用程序数据领域逻辑】的部分,通常模型对象存储和检索数据库模型的状态,比如一个产品对象可以从数据库检索信息进行操作,然后将最新信息写回到SQL Server中的产品表。

在小型的应用程序中,模型往往是概念上的分离,而不是物理上的联系。比如,如果应用程序仅仅读取一个数据集发送给视图的应用程序不具有物理模型层和相关类,在这种情况下,数据集采用一种模型对象的作用。

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.

视图:视图是【显示与用户界面交互(UI)】的部分,通常,这是创建模型数据的界面。举个例子,在一个编辑产品表的视图上显示基于产品对象当的前状态的文本框,下拉条和复选框。

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.

控制器:控制器是【处理用户交互与模型的工作,并选择合适的视图呈现给用户】的部分,在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逻辑应该交给View负责,用户交互逻辑应该controller负责,业务逻辑应该由Model负责,这样的分离帮你管理复杂的应用程序,因为它使你可以专注于某一方面的实现,比如,你可以集中精力关注视图而不用管业务逻辑。

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 Forms的应用程序中一个Page类拥有同进负责用户输入和输出两种功能,编写基于Web Forms的自动化测试程序是很复杂的,因为要测试一个页面时要实例化Page类和它全部的子控件还有额外的依赖类,由于运行网页需要对如此多的类进行实例,所以很难编写出针对应用程序某一部分的测试,另外测试基于Web Forms的应用程序时需要一个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组成部分同样更加适合并行开发,举个例子,一个开发人们开发View,另一个开发人员可以同时编写controller逻辑,第三个开发人员可以关注Model的业务逻辑。

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.)

Before you decide to use the MVC framework or the Web Forms model for a specific Web site, weigh the advantages of each approach.

你必须要认真的考虑是使用ASP.NET MVC framework呢还是ASP.net Forms模式,MVC框架并不替代原来的Web Forms模式;你可以使用任意一种框架来开发WEB应用程序。(如果你已经有了一个基于Web Forms的应用程序,那么你可以继续使用原有的开发模式进行工作)。

在你决定使用MVC框架或Web Forms模式开发之前,请认真衡量一下各自的优点。

Advantages of an MVC-Based Web Application

The ASP.NET MVC framework offers the following advantages:

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, see Front Controller on 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应用程序优点:

它能很容易的管理复杂的应用程序,将它分为model,view和controller。

它不能使用view state和服务器控件,这使得MVC框架开发人员拥有对应用程序完全控制的能力。

它使用前端控制器模式,通过单一的过程处理Web应用程序的请求,这使你可以使用丰富的URL routing来设计应用程序。更多信息可以在MSDN站点的Front Controller查看。

它对测试驱动开发(TDD)提供更好的支持。

它可以让Web开发者和页面设计人员控制自己关心的工作,从页适合大型团队的开发。

Advantages of a Web Forms-Based Web Application

The Web Forms-based framework offers the following advantages:

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, see Page Controller on 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 (the Page class, controls, and so on) are tightly integrated and usually require less code than the MVC model.

基于Web Forms Web应用程序的优点:

它可以支持事件模型并保存HTTP的状态,益于线性业务的Web应用程序开发,基于Web Forms应用程序提供了许许多多的事件和上百种的服务器控件。

它使用页面控件器模式,在单个页面中实面很多的功能。更多信息请参考MSDN站点上的Page Controller

它使用view state和基于服务器的表单,使得管理页面状态信息更加容易。

它适合小的开发团队,使用高度集成的控件来提高Web设计人员和网页设计人员的效率。

一般来说,它适合不是那么复杂的应用程序开发,因为它的页面类和控件类是合并在一起的,所以相对于MVC模式来说它需要编写的代码更少。

Features of the ASP.NET MVC Framework
ASP.NET MVC框架的特性

The ASP.NET MVC framework provides the following features:

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.

分离了应用程序的关注点,输入逻辑、业务逻辑、UI逻辑、可测试性、默认提供测试驱动开发(TDD)。MVC框架的全部的内核都是基于接口,而且可以进行mock对象测试,mock对象是模拟应用程序中真实的行为的对象。你可以使用单元测试应用程序而不必启动asp.net,这使得单元测试更加快速、灵活,您可以使用支持.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.

它是一个可扩展的框架,ASP.NET MVC框架的每个组成部分都被设计成可以很容易替换和定制。你能定义你的【视图引擎】,URL路由表,Action方法的参数形式等其它功能,ASP.NET MVC框架同样支持依赖注入(DI)和反转倒置(IOC)容器模型。DI允许你注入一类对象,而不是通过内部创建对象本身。IOC规定如果一个对象依赖另一个对象,那么第二个对象应该从外部源(如配置文件)中获得。这使得测试更加容易。

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.

拥有更大的URL映射组件,这使得你构建的应用程序的URL易于理解并便于搜索。它规定URL不能包含文件扩展名,这种URL名称模式更好的进行搜索引擎优化SEO和代表状态传输REST处理。

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.

支持ASP.NET页面(.aspx文件),用户控件(.ascx文件)和母板页(.master 文件)作为视图的模板,你能在ASP.NET MVC框架中使用ASP.NET已有的特性,象母板页、内联表达式(<%= %>),声明服务器控件、模板、数据绑定、本地化等等。

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.

支持ASP.NET特性,Asp.net MVC同样可以使用:

forms的身份验证和Windows的身份验证。

URL验证。

Membership和roles。

输出和数据缓存。

session和profile状态管理。

健康状态监控。

configuration系统和provider架构。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值