说明:随微 软ASP.NET MVC 4一起发布的还有一个框架,叫做ASP.NET Web API。目前国内关注这项技术的人似乎还很少,这方面的文章也不多见。开发Web应用程序也许可以只用MVC这样的技术,而不用这项Web API技术,但如果用了,会给你的应用程序带来极大的好处。为此,本人转载并翻译了以下这篇文章,后面还会陆续翻译该项技术的一些官方教程。大家一起学 习,共同提高。
Microsoft ASP.NET: What's This New Web API?
微软ASP.NET:新的Web API是什么?
摘自:http://www.devproconnections.com/article/asp/microsoft-aspnet-web-api-142516
Don Kiely explains why Microsoft's Web API shines as a new technology for developers
Don Kiely解释为什么微软的Web API是开发人员的一项耀眼的新技术。
Don Kiely
Dev Pro
InstantDoc ID #142516
March 08, 2012 08:00 AM
After a respite in 2011 from new product mania, Microsoft is on a
tear in 2012 to release early versions for many of its development
tools. In the past couple of weeks we've seen a preview for Visual
Studio 2011 and ASP.NET MVC 4. We also got our first look at the SQL
Server 2012 release candidate. These new releases mark an exciting time
for technology, even if it means reevaluating these technologies and
determining what skills developers should learn and focus on.
经过2011新产品狂热的一段喘息之后,微软2012年猛然发布了许多开发工具的早期版本。在前几个星期我们看到了Visual Studio
2011和ASP.NET MVC 4的预览版。我们也拿到了SQL Server
2012预发行版。这些最新发布标志着技术令人激动的时刻,尽管这仅意味着对这些技术的重新评估,并决定着开发人员应当学习和关注哪些技能。
One truly new and exciting technology for Microsoft is the Web API. I
wasn't initially enthusiastic about this API, because it seemed to be
another web service that's similar to the RESTful API. Do we really need
another API when we already have ASP.NET Web Services and Windows
Communication Foundation (WCF), or has Microsoft gone nutty about these
kinds of APIs just as it has with data-access APIs?
一项真正全新且令人兴奋的微软技术是Web
API。我最初并不热衷于这个API,因为它似乎只是类似于REST化API的另一项web服务。在已经有了ASP.NET Web
Services(ASP.NET Web服务)和Windows Communication Foundation(Windows通信基础 —
WCF)时,还真正需要另外一种API吗?或者,微软已经陷入疯狂制作各种API,就像它做数据访问API那样?
I won't address the last part of that question—Microsoft seems pretty
nutty at times—but I'm really starting to warm up to the new Web API,
particularly because it's solidified into what appears to be the actual
release API. I've come to realize that the reason for this is that the
Web API feels natural and seems to fit in with the kinds of applications
that I build. The Web API is far more than the relatively basic and
hard-to-secure ASP.NET Web Services and the mind-numbingly complex WCF
that always feels like a gross overkill for most things a website needs
to do. At least this situation is true for the kinds of applications
that I build most often these days.
我没有注意到问题的后一部分 — 微软有时确实是相当疯狂的 — 但我对这个新的Web API真正开始热衷起来了。我开始认识到,这么做的原因是Web
API感觉自然,而且它似乎适合于我所建立的各种应用程序。Web API远远超过了相对基本且难以安全的ASP.NET
Web服务,而极为复杂的WCF总让人感觉对于web网站要做的事件就像杀鸡用了牛刀。至少,对于我这些日子最经常建立的各种应用程序而言,情况确实是这
样。
I've outlined some pretty subjective reasons for why I like the new
Web API. From an objective and raw technology perspective, the Web API
is designed and lives for HTTP. One complex problem with WCF is that it
provides everything for everyone for every transport protocol, as Matt Milner explains in his blog post.
You have to do some work to use WCF over HTTP. In contrast, the Web API
is designed from the ground up to make good use of HTTP, warts and all.
The Web API isn't exactly lightweight, but with only one protocol to
support the API, it doesn't have a lot of excess baggage.
我概括了为什么喜欢这个新的Web API的一些相当主观的原因。从客观和原始技术的观点看,Web API是为HTTP而设计和存在的。WCF的一个复杂问题是它对每一个传输协议提供了方方面面的东西,就像Matt Milner在他的博客文章所解释的那样。你必须做一些工作才能将WCF运用于HTTP。与之相反,Web API毫无保留地就是围绕充分利用HTTP而设计的。Web API其实并不是轻量级的,但它只针对一个协议来支持API,它不会产生很多超重的包袱。
The other thing that I like about the Web API is that it reflects the direction in which the web is headed. As Scott Guthrie points out in the first part of his blog post series about the Web API,
many large websites are exposing Web APIs to make their services
available to third-party applications (he cites Facebook, Twitter,
LinkedIn, and Netflix as examples). He also lists a bunch of reasons why
a Web API is a good solution, including a modern HTTP programming
model. It's about time Microsoft got around to providing this model!
我喜欢Web API的另一件事是,它反映了web前进的方向。正如Scott Guthrie在他关于Web API系列文章的第一部分(中
文版网址:http://blogs.msdn.com/b/scottgu/archive/2012/03/26/asp-net-web-
api.aspx)所指出的那样,许多大型网站都暴露了Web
API,以使第三方应用程序能运用它们的服务(他例证了Facebook、Twitter、LinkedIn,以及Netflix等)。他也列出了一打理
由,以说明为什么Web API是一种好的解决方案,包括一个“现代HTTP编程模型”。现在,已经到了微软提供这种模型的时候了!
I love the close integration of the Web API with ASP.NET MVC.
Microsoft chose to implement custom Web APIs by using the
well-established controller model. This makes sense because a lot of
people were already implementing custom Web APIs by using MVC
controllers that return JSON data to the client. That's a slick
solution, and the Web API just makes it better. The only big difference
is that a Web API controller implements ApiController class instead of
Controller class, which in turn implements the IHttpController
interface. ApiController is a relatively simple class in the
System.Web.Http namespace, so it isn't tied exclusively to ASP.NET MVC,
but it understands the HTTP protocol and most of its common verbs.
我喜欢Web API与ASP.NET MVC的密切集成。微软通过运用建立良好的控制器模型来选择实现自定义Web
API。这是有意义的,因为,通过使用MVC的控制器把JSON数据返回给客户端的办法,很多人都已经实现了自定义Web
API。这是一个平滑的解决方案,而且Web API恰恰使之更好。唯一大的差别是Web
API控制器实现的是ApiController类,而不是Controller类,它转而实现了IHttpController接口。
ApiController是System.Web.Http命名空间中的一个相对简单的类,因此它并未专门绑定于ASP.NET
MVC,但它能够理解HTTP协议及其大多数常规谓词。
Best of all, the API is available to a wide variety of clients,
including mobile applications. In fact, this fact is probably its
biggest selling point. Web applications are increasingly not limited to
applications that run in a desktop browser.
最好原因是,这种API可用于十分广泛的客户端,包括移动应用程序。事实上,这一事实可能是它最大的卖点。Web应用程序已经日益不仅限于运行在桌面浏览器的应用程序了。
I'm not as big a fan of Microsoft's Web API as I am of Entity
Framework, but I can already feel the tug. It'll be interesting to see
how widely used this new technology becomes when it's finally released,
but right now Web API is looking like a winner.
我是实体框架而不是微软Web API的大粉丝,但我已经能够感觉到这种拽力。看到新技术在它最终发布时变得广泛使用是很有趣的,而现在,Web API渐渐看起来像是一个赢家。
为什么要用Web API
Web API最重要的是可以构建面向各种客户端的服务。另外与WCF REST 服务不同在于Web API利用Http协议的各个方面来表达服务(例如 URI/request response header/caching/versioning/content format),因此就省掉很多配置。
当你遇到以下这些情况的时候,就可以考虑使用Web API了。
- 需要Web Service但是不需要SOAP
- 需要在已有的WCF服务基础上建立non-soap-based http服务
- 只想发布一些简单的Http服务,不想使用相对复杂的WCF配置
- 发布的服务可能会被带宽受限的设备访问
- 希望使用开源框架,关键时候可以自己调试或者自定义一下框架
主要功能
Web API的主要功能
- 支持基于Http verb (GET, POST, PUT, DELETE)的CRUD (create, retrieve, update, delete)操作。 通过不同的http动作表达不同的含义,这样就不需要暴露多个API来支持这些基本操作。
- 请求的回复通过Http Status Code表达不同含义,并且客户端可以通过Accept header来与服务器协商格式,例如你希望服务器返回JSON格式还是XML格式。
- 请求的回复格式支持 JSON,XML,并且可以扩展添加其他格式。
- 原生支持OData。
- 支持Self-host或者IIS host。
- 支持大多数MVC功能,例如Routing/Controller/Action Result/Filter/Model Builder/IOC Container/Dependency Injection。
Asp.Net Web API 和Asp.Net MVC
你可能会觉得Web API 与MVC很类似,他们有哪些不同之处呢?
详细点说他们的区别,
- MVC主要用来构建网站,既关心数据也关心页面展示,而Web API只关注数据
- Web API支持格式协商,客户端可以通过Accept header通知服务器期望的格式
- Web API支持Self Host,MVC目前不支持
- Web API通过不同的http verb表达不同的动作(CRUD),MVC则通过Action名字表达动作
- Web API内建于ASP.NET System.Web.Http命名空间下,MVC位于System.Web.Mvc命名空间下,因此model binding/filter/routing等功能有所不同
- 最后,Web API非常适合构建移动客户端服务
Web API和WCF
发布服务在Web API和WCF之间该如何取舍呢?这里提供些简单地判断规则,
- 如果服务需要支持One Way Messaging/Message Queue/Duplex Communication,选择WCF
- 如果服务需要在TCP/Named Pipes/UDP (wcf 4.5),选择WCF
- 如果服务需要在http协议上,并且希望利用http协议的各种功能,选择Web API
- 如果服务需要被各种客户端(特别是移动客户端)调用,选择Web API
http://msdn.microsoft.com/en-us/library/jj823172.aspx
总结
又简单的进行了系列的汇总和列举,这也是一项很大的工程。暂时已经学习翻译了六个课程,感觉其中的某些知识点和Asp.Net MVC有比较高的相似度,这也让自己对MVC中的一些问题有了更为深刻的理解和认识。希望能有更多的朋友参与其中。
|
请发表评论