As part of the recent Visual Studio 2012 and .NET 4.5 launch, Microsoft formally unveiled its new web services framework called the ASP.NET Web API. Included as part of the ASP.NET MVC 4 offering, the open-source ASP.NET Web API is designed to simplify the development and consumption of RESTful services.
In a post called “Where does ASP.NET Web API Fit?”, Rick Strahl of West Wind Technologies explains the purpose and benefits of the ASP.NET Web API.
ASP.NET Web API differentiates itself from the previous Microsoft in-box HTTP service solutions in that it was built from the ground up around the HTTP protocol and its messaging semantics. Unlike WCF REST or ASP.NET AJAX with ASMX, it’s a brand new platform rather than bolted on technology that is supposed to work in the context of an existing framework. The strength of the new ASP.NET Web API is that it combines the best features of the platforms that came before it, to provide a comprehensive and very usable HTTP platform. Because it's based on ASP.NET and borrows a lot of concepts from ASP.NET MVC, Web API should be immediately familiar and comfortable to most ASP.NET developers.
Strahl points out the core capabilities that make the ASP.NET Web API a natural fit for both existing users of the the ASP.NET MVC framework, and developers who need to build HTTP endpoints.
- Strong Support for URL Routing to produce clean URLs using familiar MVC style routing semantics
- Content Negotiation based on Accept headers for request and response serialization
- Support for a host of supported output formats including JSON, XML, ATOM
- Strong default support for REST semantics but they are optional
- Easily extensible Formatter support to add new input/output types
- Deep support for more advanced HTTP features via HttpResponseMessage and HttpRequestMessage
classes and strongly typed Enums to describe many HTTP operations- Convention based design that drives you into doing the right thing for HTTP Services
- Very extensible, based on MVC like extensibility model of Formatters and Filters
- Self-hostable in non-Web applications
- Testable using testing concepts similar to MVC
Microsoft already has an existing a web services framework called Windows Communication Foundation (WCF) which lets developers build contract-first services that leverage transport protocols such as TCP, HTTP and MSMQ. Originally built for SOAP-based services that want WS-* capabilities, WCF eventually added a handful of REST-friendly capabilities. While the ASP.NET Web APIs were initially aligned to the WCF framework, they were eventually shuffled over to the ASP.NET team as described in a Code Project article by Web developer Ido Flatow.
As time passed, the WCF Web APIs had a lot of trouble adapting WCF to the “native” HTTP world. As WCF was primarily designed for SOAP-based XML messages, and the “open-heart” surgery that was required to make the Web API work as part of WCF was a bit too much (or so I understand from people who were involved in creating the Web APIs). On the other hand, the ASP.NET MVC infrastructure with its elegant handling of HTTP requests and responses, and its support of easy-to-create controllers seemed like the proper way to go for creating this new type of services.
WCF remains alive and well in the latest .NET 4.5 release and Flatow identifies some of the decision criteria for choosing between WCF and the ASP.NET Web API.
- If your intention is to create services that support special scenarios – one way messaging, message queues, duplex communication etc, then you’re better of picking WCF
- If you want to create services that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transports are unavailable, then you’re better off with WCF and using both SOAP-based bindings and the WebHttp binding.
- If you want to create resource-oriented services over HTTP that can use the full features of HTTP – define cache control for browsers, versioning and concurrency using ETags, pass various content types such as images, documents, HTML pages etc., use URI templates to include Task URIs in your responses, then the new Web APIs are the best choice for you.
- If you want to create a multi-target service that can be used as both resource-oriented service over HTTP and as RPC-style SOAP service over TCP – talk to me first, so I’ll give you some pointers.
The ASP.NET Web API is included in Visual Studio 2012 and is also available as a download for Visual Studio 2010 users. Developers looking to get started with the framework will find many example projects available on the team’s Codeplex site.