Using the ASP.NET Web API Beta
Over the last week, I was building some data services for a demo project for our non-profit subsidiary, EffectiveSC, and we decided to use this quick project as an opportunity to get familiar with the ASP.NET Web API beta that is part of the ASP.NET MVC 4 beta. My first impressions of the Web API are generally very positive. I was able to get a basic Create-Read-Update-Delete (CRUD) service up and running using the appropriate HTTP verbs (PUT, GET, POST, DELETE respectively) in just a couple of hours and the service automatically translated my model to JSON or XML based on the Accepts header in the incoming request.
You can add the ASP.NET Web API to an existing project by running the following from the NuGet Package Manager console:
1 | Install-Package AspNetWebApi |
or create a new project with the ASP.NET MVC 4 Web Application template installed with the ASP.NET MVC 4 beta.
If you create a new project, the Web API template creates a new controller, ValuesController, that inherits from the new ApiController class. This new class looks something like this:
1 | public class BadgeController : ApiController |
In my case, I was sending a EarnedBadge object as my model. In each of the methods, I simply performed the required query operations against my Entity Framework Code First data store and the framework took care of all of the serialization. The EarnedBadge object was defined as:
1 | public class EarnedBadge |
While this is a pretty simple object, the serialization resulted in the following results for JSON and XML respectively:
1 | { |
and
1 |
|
I did use the JSON.NET formatter as described by Henrik Nielsen in order to format the dates into ISO-8601 format which is a little easier for JavaScript-based clients to consume. From my perspective, one of the nicest features of using the ASP.NET Web API is the control it gives over the HTTP response messages, especially for exceptions. The new HttpResponseException allows me to control the HTTP status codes that are sent when errors occur such as the HTTP 405 Method Not Allowed for the Delete method in the example above. This allows me to build the service in a truly RESTful fashion and to send meaningful error messages back to the client.
Another really impressive feature is the built in OData query support. Any IEnumerable or IQueryable object that is returned by the service is automatically queryable using the OData query string parameter syntax which makes building paging and searching of data sets really easy.
The one feature that I really like in WCF’s REST implementation that isn’t available yet in the ASP.NET Web API is the auto-generated service API documentation. The ASP.NET Web API team has confirmed that this feature will be available in the final release. It appears that, for most of the service work that I do at EffectiveUI, the Web API will be the way to go moving forward. A good discussion of the ASP.NET Web API vs. WCF can be found at iDesign and it largely mirrors my my own thoughts on the comparison.
I hope my initial thoughts and experiences prove useful and I’d love to hear how others are using this new tool.