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:
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:
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:
public class EarnedBadge
While this is a pretty simple object, the serialization resulted in the following results for JSON and XML respectively:
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.