ASP.NET Web API是什嗎?

來源:互聯網
上載者:User

標籤:

【翻譯】ASP.NET Web API是什嗎?

說明:隨微 軟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可用於十分廣泛的用戶端,包括行動裝置 App程式。事實上,這一事實可能是它最大的賣點。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.
我是Entity Framework而不是微軟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中的一些問題有了更為深刻的理解和認識。希望能有更多的朋友參與其中。

 

ASP.NET Web API是什嗎?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.