【翻译】ASP.NET Web API是什么?

[来源] 达内    [编辑] 达内   [时间]2012-11-09

随微软ASP.NET MVC 4一起发布的还有一个框架,叫做ASP.NET Web API

说明: 随微软ASP.NET MVC 4一起发布 还有一个框架,叫做ASP.NET Web API。目前国内关注这项技术的人似乎还很少,这方面的文章也不多见。开发Web应用程序也许可以只用MVC这样的技术,而不用这项Web API技术,但如果用了,会给你的应用程序带来极大的好处。为此,本人转载并翻译了以下这篇文章,后面还会陆续翻译该项技术的一些官方教程。大家一起学习,共同提高。

< h1 style="margin: 0px; padding: 1em 0px 0.5em; font-weight: bold; color: rgb(0, 0, 0); line-height: 1.5; font-family: verdana, Arial, Helvetica, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">Microsoft ASP.NET: What's This New Web API?
微软ASP.NET:新的Web API是什么? < p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">摘自:http://www.devproconnections.com/article/asp/microsoft-aspnet-web-api-142516

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> Don Kiely explains why Microsoft's Web API shines as a new technology for developers
Don Kiely解释为什么微软的Web API是开发人员的一项耀眼的新技术。

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> WhatsAPI
Don Kiely
Dev Pro
InstantDoc ID #142516
March 08, 2012 08:00 AM

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> After a respite in 2011 from new product mania, Microsoft is on a tear in 2012 to release early versions for many of its d evelopment 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预发行版。这些最新发布标志着技术令人激动的时刻,尽管这仅意味着对这些技术的重新评估,并决定着开发人员应当学习和关注哪些技能。

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> One truly new and exciting technology for Microsoft is the Web API. I wasn't initially enthusiastic about this API, becaus e 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那样?

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> I won't address the last part of that question—Microsoft seems pretty nutty at times—but I'm really starting t o 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 relativ ely 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网站要做的事件就像杀鸡用了牛刀。至少,对于我这些日子最经常建立的各种应用程序而言,情况确实是这样。

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 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,它不会产生很多超重的包袱。

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 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系列文章 的第一部分所指出的那样,许多大型网站都暴露了Web API,以使第三方应用程序能运用它们的服务(他例证了Facebook、Twitter、LinkedIn,以及Netflix等)。他也列出了一打理由,以说明为什么Web API是一种好的解决方案,包括一个“现代HTTP编程模型”。现在,已经到了微软提供这种模型的时候了!

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> I love the close integration of the Web API with ASP.NET MVC. Microsoft chose to implement custom Web APIs by using the we ll-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 implem ents 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协议及其大多数常规谓词。

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 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应用程序已经日益不仅限于运行在桌面浏览器的应用程序了。

< p style="margin: 10px auto; padding: 1em 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 25px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 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看起来像一个赢家。

资源下载