DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Consuming ASP.NET MVC Web API

Consuming ASP.NET MVC Web API

Sumit Dutta user avatar by
Sumit Dutta
·
Jun. 26, 12 · Mobile Zone · Interview
Like (0)
Save
Tweet
6.49K Views

Join the DZone community and get the full member experience.

Join For Free

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.

ASP.NET MVC API Web API ASP.NET Web Service

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Most Popular Kubernetes Alternatives and Competitors
  • 10 Steps to Become an Outstanding Java Developer
  • 6 Things Startups Can Do to Avoid Tech Debt
  • How to Submit a Post to DZone

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo