ASP.NET MVC 3技术

r01cn 2011-09-24 05:40:11
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标记,但仍残留有许多内在局限性。

...全文
251 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
r01cn 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 fulans1 的回复:]

楼主应该是个人品不错的人
[/Quote]

shy...
r01cn 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 r01cn 的回复:]

期待更多的指正....
[/Quote]

shy...
倾城之恋 2011-09-24
  • 打赏
  • 举报
回复
楼主应该是个人品不错的人
r01cn 2011-09-24
  • 打赏
  • 举报
回复
期待更多的指正....
r01cn 2011-09-24
  • 打赏
  • 举报
回复
呵呵,能有人这样斟酌真好,谢谢了
threenewbee 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 r01cn 的回复:]
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 ……
[/Quote]
嗯,是的。

我觉得良好的翻译应该在忠于原文和尊重目的语言本身习惯两者之间做出最佳选择。当然,作为翻译我是业余水平,但是这不妨碍我在阅读译作的时候有些挑剔——即便是专业的翻译作者,很多地方也不让我满意。
r01cn 2011-09-24
  • 打赏
  • 举报
回复
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.

ASP.NET MVC是微软的一种web开发框架,它整合了“模型-视图-控制器(MVC)”体系结构的效率与简洁、敏捷开发最现代的思想与技术、以及当前ASP.NET平台最好的部分。

似乎可以这样翻译更好些?
threenewbee 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 r01cn 的回复:]
“不要责备我咬文嚼字,看书一定要看懂,而不是用翻译软件草草翻译了事。”

呵呵,这句不实,我到完全是自己翻译的,根本没用什么翻译软件
[/Quote]
嗯。良好的机器翻译和蹩脚的人工翻译的界限已经模糊不清。
r01cn 2011-09-24
  • 打赏
  • 举报
回复
“不要责备我咬文嚼字,看书一定要看懂,而不是用翻译软件草草翻译了事。”

呵呵,这句不实,我到完全是自己翻译的,根本没用什么翻译软件
threenewbee 2011-09-24
  • 打赏
  • 举报
回复
ASP.NET MVC是微软一个web开发框架,它整合了模型-视图-控制器(MVC)体系结构的效率和整洁,灵活开发最流行的思想与技术,是目前ASP.NET平台最好的部分
但愿你注意到红色标出来你的翻译中的偏差。但是至少在我看来,它的意思是不正确的。
r01cn 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 caozhy 的回复:]

这句话正确的含义是MVC是一个Web开发框架,它结合了MVC架构的高效和简洁,敏捷开发的思想和技术,以及ASP.NET平台其它部分的最佳功能。

[/Quote]

不错,“以及”是关键,你这样翻译确实比我的更好些。但我觉得意思上还是没什么大错。
threenewbee 2011-09-24
  • 打赏
  • 举报
回复
意思错了,理解就错了。

比如 “agile development”,敏捷开发是MVC很重要的价值,包括TDD,包括DDD,几乎MVC的书,本质上都是在讲敏捷开发过程。如果看一本书把自己不懂的都忽略掉,看书还有意义么?

再比如 “and the best parts of the existing ASP.NET platform”,其实它在说这么一个特性,那就是 ASP.NET MVC 基于 ASP.NET 平台,并且原先 ASP.NET 很多重要特性,比如视图引擎、权限验证,这些都可以在 MVC 中利用,这也是很重要的特性。

不要责备我咬文嚼字,看书一定要看懂,而不是用翻译软件草草翻译了事。
threenewbee 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 r01cn 的回复:]
引用 4 楼 caozhy 的回复:

英文不错,中文不通。

比如:“ASP.NET MVC是微软一个web开发框架,它整合了模型-视图-控制器(MVC)体系结构的效率和整洁,灵活开发最流行的思想与技术,是目前ASP.NET平台最好的部分。”

你找一个中国人,念给他听听。


英文是原文,中文是我对原文的翻译,有错误是肯定的。不过,你所指的这一句,我到是觉得没什么大错,最大的……
[/Quote]

这句话正确的含义是MVC是一个Web开发框架,它结合了MVC架构的高效和简洁,敏捷开发的思想和技术,以及ASP.NET平台其它部分的最佳功能。

你知道你为什么翻译的不通吗?因为你没有理解这句话的意思。另外也没有翻译出几个关键技术词汇的含义。
r01cn 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 caozhy 的回复:]

英文不错,中文不通。

比如:“ASP.NET MVC是微软一个web开发框架,它整合了模型-视图-控制器(MVC)体系结构的效率和整洁,灵活开发最流行的思想与技术,是目前ASP.NET平台最好的部分。”

你找一个中国人,念给他听听。
[/Quote]

英文是原文,中文是我对原文的翻译,有错误是肯定的。不过,你所指的这一句,我到是觉得没什么大错,最大的错误是少了一个字:“ASP.NET MVC是微软一个开发框架....”。要不,你对原文的这句翻译一下试试?让我看看你的中文有多通~~
MSDNXGH 2011-09-24
  • 打赏
  • 举报
回复
占个位
threenewbee 2011-09-24
  • 打赏
  • 举报
回复
英文不错,中文不通。

比如:“ASP.NET MVC是微软一个web开发框架,它整合了模型-视图-控制器(MVC)体系结构的效率和整洁,灵活开发最流行的思想与技术,是目前ASP.NET平台最好的部分。”

你找一个中国人,念给他听听。
叶子 2011-09-24
  • 打赏
  • 举报
回复
占个位置,楼主继续!
r01cn 2011-09-24
  • 打赏
  • 举报
回复
前几天一直不知道怎么发帖子,今天终于知道怎么做了,发个试试....

这是本人学习ASP.NET MVC 3时对原文所做的一点翻译。

呵呵,本贴不“结贴”,回贴也不给分,俺的积分少得太可怜了...,到是希望这类技术介绍性的帖子版主应该适当给楼主一点积分~~
r01cn 2011-09-24
  • 打赏
  • 举报
回复
Web Development Today
当今的Web开发


Outside Microsoft, web development technology has been progressing rapidly and in several different directions since Web Forms was first released. Aside from AJAX, which we’ve already noted, there have been other major developments.
微软之外,自Web表单首次发布以来,web开发技术发展迅速并表现在几个不同的方向。除了我们已经提及的AJAX以外,还有一些其它的主要发展。

Web Standards and REST
Web标准与REST


The drive for web standards compliance has increased in recent years. Web sites are consumed on a greater variety of devices and browsers than ever before, and web standards (for HTML, CSS, JavaScript, and so forth) remain our one great hope for enjoying a decent browsing experience everywhere—even on the Internet-enabled refrigerator. Modern web platforms can’t afford to ignore the business case and the weight of developer enthusiasm for web standards compliance.
向web标准靠拢近年来已经增强。Web网站建立在比以往更广泛的设备和浏览器之上,web标准(HTML、CSS、JavaScript等等)使我们有望能够在任何地方 — 甚至在连网的冰箱上 — 都可以有相当好的浏览体验。现代web平台不可忽视商业环境和开发者对顺应web标准的挚爱。

At the same time, Representational State Transfer (REST) has become the dominant architecture for application interoperability over HTTP, completely overshadowing SOAP (the technology behind ASP.NET’s original approach to web services). REST describes an application in terms of resources (URIs) representing real-world entities and standard operations (HTTP methods) representing available operations on those resources. For example, you might PUT a new http://www.example.com/ Products/Lawnmower or DELETE http://www.example.com/Customers/Arnold-Smith.
与此同时,表现式状态传输(REST)已经成为应用程序在HTTP上互操作性的主要体系结构,完全使SOAP(ASP.NET原先进行web服务的支撑技术)黯然失色。REST根据表示真实世界实体的资源(URI)和表示在这些资源上可用的标准操作(HTTP方法)来描述一个应用程序。例如,你可能会PUT(递交)一个新的http://www.example.com/Products/Lawnmower,或DELETE(删除)http://www.example.com/Customers/Arnold-Smith。

Today’s web applications don’t serve just HTML; often they must also serve JSON or XML data to various client technologies including AJAX, Silverlight, and native smartphone applications. This happens naturally with REST, which eliminates the historical distinction between web services and web applications—but requires an approach to HTTP and URL handling that has not easily been supported by ASP.NET Web Forms.
当今的web应用程序不只服务于HTML,通常它们也必须把JSON或XML数据提供给各种客户端技术,包括AJAX、Silverlight,以及本地智能手机应用程序。利用REST,这会自然发生。REST消除了历史上web服务与web应用程序之间的差异 — 但这需要一种对HTTP和URL进行处理的办法,而这种办法对ASP.NET Web表单而言却难以得到支持。

Agile and Test-Driven Development
灵活及测试驱动的开发

It is not just web development that has moved on in the last decade—software development as a whole has shifted towards agile methodologies. This can mean a lot of different things, but it is largely about running software projects as adaptable processes of discovery, resisting the encumbrance and restrictions of excessive forward planning. Enthusiasm for agile methodologies tends to go hand in hand with a particular set of development practices—and tools (usually open source) that promote and assist these practices.
在过去的十年中不只web开发得到了发展 — 作为一个整体的软件开发也朝着灵活的方法学方向前进。这可能意指许多不同的事情,但主要是指以发现、抵制阻碍和过分计划的自适应处理方式运作软件项目。灵活方法学的热情倾向于把一组特定的开发实践与促进和辅助这些实践的工具(通常是开源的)结合在一起。

Test-driven development (TDD), and its latest reincarnation, behavior-driven development (BDD), are two obvious examples. The idea is to design your software by first describing examples of desired behaviors (known as tests or specifications), so at any time you can verify the stability and correctness of your application by executing your suite of specifications against the implementation. There’s no shortage of .NET tools to support TDD/BDD, but these tend not to work well with Web Forms:
测试驱动开发(TDD),以及它最新的化身,行为驱动开发(BDD)是两个明显的例子。其思想是通过首先描述想得到的行为的示例(称为测试或规约)的办法来设计你的软件,所以,任何时刻,你都可以根据(已经形成的)实现(implementation)执行你的一套规约来检验你应用程序的稳定性和正确性。.NET支持TDD/BDD的工具并不短缺,但这些不能与web表单很好地工作:

Unit testing tools let you specify the behavior of individual classes or other small code units in isolation. These can only be effectively applied to software that has been designed as a set of independent modules, so that each test can be run in isolation. Unfortunately, few Web Forms applications can be tested this way. Following the framework’s guidance to put logic into event handlers or even use server controls that directly query databases, developers typically end up tightly coupling their own application logic to the Web Forms runtime environment. This is death for unit testing.
单元测试工具使你可以规定个别类或其它孤立的小型代码单元的行为。这些只能被有效地运用于被设计成一组独立模块的软件,以使得每个测试可独立地运行。不幸的是,很少有web表单应用程序能够以这种方式进行测试。根据(Web表单)框架指南,要把逻辑放到事件处理程序中,或者甚至使用直接数据库查询的服务器控件,开发者最终典型地需要把自己的应用程序逻辑与web表单运行时环境紧紧地耦合在一起。这是不能进行单元测试的死结。

UI automation tools let you simulate a series of user interactions against a complete running instance of your application. These can in theory be used with Web Forms, but they can break down whenever you make a slight change to your page layout. Without special attention, Web Forms starts generating totally different HTML structures and element IDs, rendering your existing test suite useless.
UI自动化工具使你能够根据应用程序的一个复杂的运行实例来模拟一系列用户交互。这在理论上可以用于web表单,但当你对你的页面布局作微小改动时,它们都可能会跨掉。如果不特别小心,web表单就会生成完全不同的HTML结构和元素ID,导致你正使用的测试套件失效。

The .NET open source and independent software vendor (ISV) community has produced no end of top-quality unit testing frameworks (NUnit, xUnit), mocking frameworks (Moq, Rhino Mocks), inversion-of-control containers (Ninject, AutoFac), continuous integration servers (Cruise Control, TeamCity), object-relational mappers (NHibernate, Subsonic), and the like; and proponents of these tools and techniques have found a common voice, publishing and organizing conferences under the shared brand ALT.NET. Traditional ASP.NET Web Forms is not amenable to these tools and techniques because of its monolithic design, so from this vocal group of experts and industry thought leaders, Web Forms gets little respect.
.NET开源和独立的软件厂商团体已经生产了无数顶级质量的单元测试框架(NUnit、xUnit)、模仿框架(Moq、Rhino Mocks)、逆转控制容器(Ninject、AutoFac)、连续累计服务器(Cruise Control、TeamCity)、对象关系映射器(NHibemate、Subsonic)等等。而且这些工具和技术的建议者们已经在共享商标ALT.NET下形成了共同的意见、发表并组织会议。传统的ASP.NET Web表单由于它的单片式设计,不服从于这些工具和技术。因此,在这在专家团体和行业思想领导者们的眼中,Web表单没能得到起码的尊重。

Ruby on Rails

In 2004, Ruby on Rails was a quiet, open source contribution from an unknown player. Suddenly fame hit, transforming the rules of web development. It’s not that it contained revolutionary technology, but that it took existing ingredients and blended them in such a compelling and appealing way as to put existing platforms to shame.
2004年Ruby on Rails还默默无闻,只是一个无名倡导者的开源项目。突然声名鹤戾,转变着web开发的规则。并不是它包含了革命性的技术,而是它利用了已有的成分并把它们掺和成一种引人注目的方式而使现在的平台感到羞愧。

Ruby on Rails (or just Rails as it is commonly called) embraced a model-view-controller (MVC) architecture. Don’t worry if you are not familiar with the MVC pattern—we’ll explain the details as we go. By applying MVC and working in tune with the HTTP protocol instead of against it, by promoting conventions instead of the need for configuration, and by integrating an object-relational mapping (ORM) tool into its core, Rails applications more or less fell into place without much effort. It was as if this was how web development should have been all along; as if we’d suddenly realized we’d been fighting our tools all these years and now the war was over. Rails shows that web standards compliance and RESTfulness don’t have to be hard. It also shows that agile development and TDD work best when the framework is designed to support them. The rest of the web development world has been catching up ever since.
Ruby on Rails(或它通常所称的Rails)信奉一种模型-视图-控制器体系结构。如果你不熟悉MVC模式不用着急 — 我们接下来会详细解释。通过运用MVC并配合而不是违背HTTP协议进行工作,用促进规范代替需要配置,以及把面向对象映射工具集成到它的内核,Rails应用程序依序渐进而不需要过多努力。就好像如何进行web开发的日子已经来了,好像我们突然地意识到所有这些年我们一直在进行工具战争,现在,战争结束了。Rails表明顺应web标准与REST化并不难。它也表明当框架设计成支持灵活开发与TDD时,它们配合工作是最好的。从那时起,整个web开发世界都被调动起来了。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧