Introduction

This code sample demonstrates how to consume a RESTful web api in a Windows Metro-style application written in c#.
It includes user interface for allowing a user to carry out CRUD (create/read/update/delete) operations on a collection of 'person' objects. This sample is not intended to be a good example of developing a RESTful API as it is just sufficient to be able to show how it is consumed. Design time data is also included.

Building the Sample

No special requirements for building.

NOTE: If you are using IISExpress 8 there is an issue whereby PUT and DELETE's will return a 404. This can be fixed by the following:

Open file %userprofile%\documents\iisexpress\config\applicationhost.config and add PUT,DELETE verbs into the ExtensionlessUrl-Integrated-4.0 setting
(see http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-vs2012rc-iis-8-express)

 

Description
This sample illustrates very simply how to consume a RESTful web api in a Windows Metro-style application written in c# using HttpClient. The sample contains two projects; one for the Web API itself and another for the Metro client application.
The Web API was created as follows:
C#
Edit|Remove
// GET /api/person 
public IEnumerable<Person> Get() 
{ 
    return List; 
} 
 
// GET /api/person/5 
public Person Get(int id) 
{ 
    return List.SingleOrDefault(p => p.Id == id); 
} 
 
// POST /api/person 
public void Post(Person value) 
{ 
    List.Add(value); 
} 
 
// PUT /api/person/5 
public void Put(int id, Person value) 
{ 
    var item = List.SingleOrDefault(p => p.Id == id); 
    if (item != null) 
    { 
        item.Name = value.Name; 
        item.Occupation = value.Occupation; 
    } 
} 
 
// DELETE /api/person/5 
public void Delete(int id) 
{ 
    var item = List.SingleOrDefault(p => p.Id == id); 
    if (item != null) 
    { 
        List.Remove(item); 
    } 
} 
The Metro client application was created as follows:
The MainViewModel contains code to consume the Web API using HttpClient and the respective Http verbs.
C#
Edit|Remove
// Getting the list.. 
using (var http = new HttpClient()) 
{ 
    var resp = await http.GetAsync(new Uri(ApiRoot)); 
    using (var stream = await resp.Content.ReadAsStreamAsync()) 
    { 
        var djs = new DataContractJsonSerializer(typeof (List<Person>)); 
        People = new ObservableCollection<Person>((IEnumerable<Person>) djs.ReadObject(stream)); 
    } 
} 
 
// Deleting an object.. 
using (var http = new HttpClient()) 
{ 
    var resp = await http.DeleteAsync(new Uri(ApiRoot + "/" + Selected.Id)); 
    resp.EnsureSuccessStatusCode(); 
} 
 
// Adding and updating objects.. 
using (var http = new HttpClient()) 
using (var ms = new MemoryStream()) 
{ 
    var djs = new DataContractJsonSerializer(typeof (Person)); 
    djs.WriteObject(ms, person); 
    ms.Position = 0; 
    var sc = new StreamContent(ms); 
    sc.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); 
 
    var resp = original == null 
                   ? await http.PostAsync(new Uri(ApiRoot), sc) 
                   : await http.PutAsync(new Uri(ApiRoot + "/" + original.Id), sc); 
 
    resp.EnsureSuccessStatusCode(); 
} 
 
 

This shows the main page.
 
This shows the design time data.
Source Code Files

More Information

http://babaandthepigman.wordpress.com/2012/03/25/metro-c-webapi-client/