Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Consuming ASP.NET MVC Web API

DZone's Guide to

Consuming ASP.NET MVC Web API

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

In my previous article ASP.NET MVC 4 - Hello Web API I talked about simple Web API of ASP.NET MVC 4. In this article I will talk about how to consume Web API in Windows Phone. This article will be like my previous article Windows Phone - Consume JSON Service but in this article I will use JSON.NET instead of DataContractJsonSerializer

Let's write code...

Step 1: Download code from ASP.NET MVC 4 - Hello Web API or you can create Web API application following the steps.

Step 2: Download JSON.NET from CodePlex

Step 3: Unzip downloaded JSON.NET, browse to WindowsPhone folder. You will get Newtonsoft.Json.dll in the folder.

Windows Phone JSON.NET

Step 4: Create Silverlight for Windows Phone project.

Step 5: Place Grid RowDefinition, Button, TextBlock and ListBox inside ContentPanel in MainPage.XAML.

<Grid.RowDefinitions>
   <RowDefinition></RowDefinition>
   <RowDefinition></RowDefinition>
   <RowDefinition></RowDefinition>
   <RowDefinition></RowDefinition>
</Grid.RowDefinitions>

<Button x:Name="btnGetData" Grid.Row="0" Height="75" Content="Get Web API Data" Click="btnGetData_Click" />
<TextBlock Text="Customer ID Customer Name" Height="50" Grid.Row="1" FontSize="22" FontWeight="Bold"TextAlignment="Left" Foreground="#FFFFB090"></TextBlock>
<ListBox Name="lstCustomer" Grid.Row="2" FontSize="24">
</ListBox>

Step 6: Create a Class Customer with the entities you like to bind. The Web API Json will have CustomerId, CustomerName and Address entities, in this case I am binding CustomerId and CustomerName only.

The name of entities in Customer class should be same as the name of entities returned by Web API in Json.

public class Customer
{
   public int CustomerId { get; set; }
   public string CustomerName { get; set; }
}

Step 7: Place btnGetData_Click in MainPge.Xaml.cs which will be invoked on click of GET Web API data.

void btnGetData_Click(object sender, RoutedEventArgs e)
{
   try
   {
      WebClient webClient = new WebClient();
      Uri uri = new Uri("http://localhost:1712/api/customer/");
      webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
      webClient.DownloadStringAsync(uri);
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}

Step 8: Add reference of Newtonsoft.Json.dll from WindowsPhone folder and add Newtonsoft.Json using directive.

using Newtonsoft.Json;

Step 9: Place webClient_DownloadStringCompleted method in MainPage.Xaml.cs which will be invoked when service will be completed.

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
   try
   {
      List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(e.Result);
      foreach (Customer em in customers)
      {
         int customerId = em.CustomerId;
         string customerName = em.CustomerName;
         lstCustomer.Items.Add(customerId + " " + customerName);
      }
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}

Step 10: Now run the application and click on Get Web API Data. The data will be displayed on Windows Phone application.


Windows Phone JSON.NET Web API


This ends the article of consuming Web API in Windows Phone.

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}