ASP.NET MVC is a web development framework from Microsoft that combines the effectiveness and tidiness of model-view-controller (MVC) architecture, the most up-to-date ideas and techniques from agile development, and the best parts of the existing ASP.NET platform. It’s a complete alternative to traditional ASP.NET Web Forms, delivering considerable advantages for all but the most trivial of web development projects. In this chapter, you’ll learn why Microsoft originally created ASP.NET MVC, how it compares to its predecessors and alternatives, and, finally, what’s new in ASP.NET MVC 3.
ASP.NET MVC是微软一个web开发框架,它整合了模型-视图-控制器(MVC)体系结构的效率和整洁,灵活开发最流行的思想与技术,是目前ASP.NET平台最好的部分。它是传统的ASP.NET Web表单的一种完善的替代品,即使是对最微不足道的web项目,它都具备了相当的优势。在本章中,你将学习为什么微软当初要生成ASP.NET MVC,与它的前辈和替代品比较如何,最后介绍ASP.NET MVC 3的新特性。
A Brief History of Web Development
Web开发简史
To understand the distinctive aspects and design goals of ASP.NET MVC, it’s worth considering the history of web development so far—brief though it may be. Over the years, Microsoft’s web development platforms have demonstrated increasing power—and (unfortunately) increasing complexity. As shown in Table 1–1, each new platform tackled the specific shortcomings of its predecessor.
要理解ASP.NET MVC的特色及其设计目标,有必要考察一下web开发的历史 — 虽然这很简短。过去几年,微软的web开发平台演变得越加强大 — 也(不幸的是)越加复杂。如表1-1所示,每一个新的平台都捉住了它前任的缺点。
Traditional ASP.NET Web Forms
传统的ASP.NET Web表单
ASP.NET was a huge shift when it first arrived in 2002. Figure 1-1 illustrates Microsoft’s technology stack as it appeared then.
ASP.NET在它2002年刚问世时是一个巨大的转移。图1-1描述了它出现时微软的技术堆栈。
With Web Forms, Microsoft attempted to hide both HTTP (with its intrinsic statelessness) and HTML (which at the time was unfamiliar to many developers) by modeling the user interface (UI) as a hierarchy of server-side control objects. Each control kept track of its own state across requests (using the View State facility), rendering itself as HTML when needed and automatically connecting client-side events (e.g., a button click) with the corresponding server-side event handler code. In effect, Web Forms is a giant abstraction layer designed to deliver a classic event-driven GUI over the Web.
利用web表单,微软试图通过把用户接口(UI)模拟为服务器端控件对象层的办法把HTTP(带有无状态本质)和HTML(当时许多开发人员尚不熟悉)都隐藏起来。每个控件都保持跨请求地跟踪自己的状态(利用视图状态功能)、把自己渲染成HTML、以及自动地把客户端事件与相应的服务器端事件处理代码相关联。结果是,web表单成了被设计用来在Web上递送传统的事件驱动GUI(图形用户接口)的一个巨大的抽象层。
The idea was to make web development feel just the same as Windows Forms development. Developers no longer had to work with a series of independent HTTP requests and responses—we could now think in terms of a stateful UI. We could forget about the Web and its stateless nature and instead build UIs using a drag-and-drop designer and imagine, or at least pretend, that everything was happening on the server.
这种思想是让web开发感觉上与Windows窗口开发相同。开发者不再必须以一系列独立的HTTP请求和响应进行工作 — 现在可以把它说成是有状态的UI。我们可以忘记Web及其无状态本质,转而用一个拖-放设计器来建立各种UI,并假想,或至少假装,所有事情都发生在服务器上。
What’s Wrong with ASP.NET Web Forms?
ASP.NET Web表单有什么问题?
Traditional ASP.NET Web Forms was a great idea, but reality proved more complicated. Over time, the use of Web Forms in real-world projects highlighted some shortcomings:
传统的ASP.NET Web表单是一种伟大的思想,但事实上更加复杂。随着时间的推移,在实际项目中使用Web表单突出表现出了一些缺陷:
View State weight: The actual mechanism for maintaining state across requests (known as View State) results in large blocks of data being transferred between the client and server. This data can reach hundreds of kilobytes in even modest web applications, and it goes back and forth with every request, frustrating site visitors with slower response times and increasing the bandwidth demands of the server.
视图状态重量:跨请求的状态维护的实际机制(称为视图状态)导致在客户端与服务器之间传递大块数据。这种数据即使在最适度的web应用程序中也达到几百K,而且它来回于每次请求,以很慢的响应时间阻挠着网站的访问者,且增加了服务器带宽的需求。
Page life cycle: The mechanism for connecting client-side events with server-side event handler code, part of the page life cycle, can be extraordinarily complicated and delicate. Few developers have success manipulating the control hierarchy at runtime without getting View State errors or finding that some event handlers mysteriously fail to execute.
页面生命周期:连接客户端事件与服务器端事件处理代码的机制(是页面的部分生命周期)格外复杂和棘手。很少有开发者能够成功地维护控件层而不产生视图状态错误,或不发现某些事件处理程序莫明其妙地不能执行。
False sense of separation of concerns: ASP.NET’s code-behind model provides a means to take application code out of its HTML mark-up and into a separate code-behind class. This has been widely applauded for separating logic and presentation, but in reality developers are encouraged to mix presentation code (e.g., manipulating the server-side control tree) with their application logic (e.g., manipulating database data) in these same monstrous code-behind classes. The end result can be fragile and unintelligible.
关注分离的错误感觉:ASP.NET的后台代码模型提供了一种方法把应用程序代码从HTML标记中提取出来,而形成一个独立的后台代码类。这对逻辑与表现的分离得到了广泛的赞许,但事实上,在这些同样怪异的后台代码类中却又鼓励开发者把表现代码(如维护服务器端控件树)与它们的应用程序逻辑(如维护数据库数据)混合在一起。最终结果是脆弱和莫明其妙。
Limited control over HTML: Server controls render themselves as HTML, but not necessarily the HTML you want. Prior to ASP.NET 4, the HTML output usually failed to comply with web standards or make good use of CSS, and server controls generated unpredictable and complex ID values that are hard to access using JavaScript. These problems are reduced in ASP.NET 4, but it can still be tricky to get the HTML you expect.
HTML上的受限控件:服务器控件把自己渲染成HTML,但并不是你所想象的必要的HTML。在ASP.NET 4之前,这种HTML输出通常不符合web标准或不能很好地使用CSS,而且服务器控件生成不可预知且复杂的ID值,使得用JavaScript难以访问。这些问题在ASP.NET 4中减少了,但它仍然不容易得到你所期望的HTML。
Leaky abstraction: Web Forms tries to hide away HTML and HTTP wherever possible. As you try to implement custom behaviors, you frequently fall out of the abstraction, which forces you to reverse-engineer the postback event mechanism or perform obtuse acts to make it generate the desired HTML. Plus, all this abstraction can act as a frustrating barrier for competent web developers.
遗漏抽象:Web表单试图尽可能隐去HTML和HTTP。当你试图实现自定义行为时,你会经常性地放弃抽象,这迫使你返回到回递事件工作机制或采取笨拙的行为,以使它生成所希望的HTML。再则,这种抽象完全有可能对有能力的web开发者构成一个令人沮丧的障碍。
Low testability: The designers of ASP.NET could not have anticipated that automated testing would become an essential component of software development. Not surprisingly, the tightly coupled architecture they designed is unsuitable for unit testing. Integration testing can be a challenge too, as we’ll explain in a moment.
低可测试性:ASP.NET的设计者们没有预期到自动测试会成为软件开发的主要成分。不必奇怪,它们设计的这种紧耦合体系结构不适合单元测试。集成测试又太具挑战性,我们马上会解释。
ASP.NET has kept moving. Version 2.0 added a set of standard application components that can reduce the amount of code you need to write yourself. The AJAX release in 2007 was Microsoft’s response to the Web 2.0/AJAX frenzy of the day, supporting rich client-side interactivity while keeping developers’ lives simple. The most recent release, ASP.NET 4, produces more predictable and standards-compliant HTML markup, but many of the intrinsic limitations remain.
ASP.NET在不断改进。版本2.0添加了一组标准的应用程序组件,可以使你减少需要编写的代码量。2007年发布的AJAX是微软对Web 2.0/AJAX疯狂时代的回应,它支持富客户端交互性而使开发者的工作简单。最新版的ASP.NET 4产生更可预测且与标准兼容的HTML标记,但仍残留有许多内在局限性。