Using the ASP.NET Web API Beta

Monday, April 2, 2012 3:21 PM

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:

PM> 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:

public class BadgeController : ApiController
    // GET /api/badge
    public IEnumerable<EarnedBadge> Get()
        RelationalDataModel data = new RelationalDataModel();
        return data.EarnedBadges;

    // GET /api/badge/5
    public EarnedBadge Get(long id)

    // POST /api/badge
    public void Post(EarnedBadge value)

    // PUT /api/badge/5
    public long Put(EarnedBadge value)

    // DELETE /api/badge/5
    public void Delete(int id)
        throw new HttpResponseException(HttpStatusCode.MethodNotAllowed);

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
    public long EarnedBadgeId { get; set; }
    public long ProviderId { get; set; }
    public long BadgeId { get; set; }
    public long UserId { get; set; }
    public DateTime EarnedDateTime { get; set; }
    public string Note { get; set; }

While this is a pretty simple object, the serialization resulted in the following results for JSON and XML respectively:



<?xml version="1.0" encoding="utf-8"?>

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.

By: Shane


  • not fake 9/22/20139:32 PM In order to comply with the lender's policies, the possible homeowner should gather documents making appropriate copies for your mortgage lender or bank files not fake in case you purchased your automobile within the last 1 . 5 years, you could be capable to beat your former interest through a refinance car loan.