ASP.NET Web API - Screencast series Part 2: Getting Data

Posted by Jon Galloway on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jon Galloway
Published on Fri, 16 Mar 2012 18:54:47 GMT Indexed on 2012/03/18 17:58 UTC
Read the original article Hit count: 648

Filed under:
|
|

We're continuing a six part series on ASP.NET Web API that accompanies the getting started screencast series. This is an introductory screencast series that walks through from File / New Project to some more advanced scenarios like Custom Validation and Authorization. The screencast videos are all short (3-5 minutes) and the sample code for the series is both available for download and browsable online. I did the screencasts, but the samples were written by the ASP.NET Web API team.

In Part 1 we looked at what ASP.NET Web API is, why you'd care, did the File / New Project thing, and did some basic HTTP testing using browser F12 developer tools.

This second screencast starts to build out the Comments example - a JSON API that's accessed via jQuery. This sample uses a simple in-memory repository. At this early stage, the GET /api/values/ just returns an IEnumerable<Comment>. In part 4 we'll add on paging and filtering, and it gets more interesting.

 

The get by id (e.g. GET /api/values/5) case is a little more interesting. The method just returns a Comment if the Comment ID is valid, but if it's not found we throw an HttpResponseException with the correct HTTP status code (HTTP 404 Not Found). This is an important thing to get - HTTP defines common response status codes, so there's no need to implement any custom messaging here - we tell the requestor that the resource the requested wasn't there. 

public Comment GetComment(int id) 
{ 
    Comment comment; 
    if (!repository.TryGet(id, out comment)) 
        throw new HttpResponseException(HttpStatusCode.NotFound); 
    return comment; 
} 

This is great because it's standard, and any client should know how to handle it. There's no need to invent custom messaging here, and we can talk to any client that understands HTTP - not just jQuery, and not just browsers.

But it's crazy easy to consume an HTTP API that returns JSON via jQuery. The example uses Knockout to bind the JSON values to HTML elements, but the thing to notice is that calling into this /api/coments is really simple, and the return from the $.get() method is just JSON data, which is really easy to work with in JavaScript (since JSON stands for JavaScript Object Notation and is the native serialization format in Javascript).

$(function() { 
    $("#getComments").click(function () { 
        // We're using a Knockout model. This clears out the existing comments. 
        viewModel.comments([]); 
 
        $.get('/api/comments', function (data) { 
            // Update the Knockout model (and thus the UI) with the comments received back  
            // from the Web API call. 
            viewModel.comments(data); 
        }); 
 
    }); 
});

That's it! Easy, huh? In Part 3, we'll start modifying data on the server using POST and DELETE.

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about ASP.NET MVC