Creating First HTTP Service using ASP.NET Web API: Part1 of Many
Consuming ASP.NET Web API Service using HttpClient: Part2 of Many
How to Self-Host ASP.Net Web API: Part 3 of Many
In this post we will consume ASP.Net Web API HTTP Service in Windows Phone Application. If you do not specify header information in HTTP request then by default ASP.Net Web API service returns JSON data. So essentially there are three steps involved in consuming HTTP Service from ASP.Net Web API in Windows Phone Application.
- Make a HTTP Request
- Read data from HTTP Response
- Parse returned JSON data and bind data to data bound control
In this post we are going to consume service created in this POST
I have created a Windows Phone Application by choosing target framework 7.1. We are going to display Bloggers Data returned from ASP.Net Web API service in a ListView. Essentially we are going to consume a JSON Data from a HTTP Service. For that add below references in the project.
We are going to de-serialize JSON data using DataContractJsonSerializer class. We need to create a class at client side representing Bloggers entity from the service. Let us create a class called BloggersClient. We will de-serialize JSON data returning from the HTTP service in this object of this class.
namespace WindowsPhoneClient { public class BloggersClient { public string Id { get; set; } public string Name { get; set; } public string AreaOfIntrest { get; set; } } }
Once the client class is in placed add following references,
We are going to download JSON data from ASP.Net Web API service using WebClient class. Make sure that you are passing URL of your HTTP Service.
In the completed event, we will parse returned JSON using DataContractJsonSerializer class. In following code,
- Creating MemoryStream form returned result
- Creating object to de-serialize the returned JSON data
- Reading de-serialized data object in List of BloggersClient
- Setting List of BloggerClient as ItemSource of ListBox. On the XAML we have put a ListBox with name lstBloggers.
Code behind to load data from ASP.Net Web API HTTP Service on page load of the application and s display in ListBox is as following
using System; using System.Collections.Generic; using System.Net; using Microsoft.Phone.Controls; using System.Runtime.Serialization.Json; using System.IO; using System.Text; namespace WindowsPhoneClient { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); WebClient proxy = new WebClient(); proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted); proxy.DownloadStringAsync(new Uri("http://localhost:39192/api/Bloggers")); } void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { Stream s = new MemoryStream(Encoding.Unicode.GetBytes(e.Result)); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<BloggersClient>)); List<BloggersClient> result = (List<BloggersClient>) ser.ReadObject(s); lstBloggers.ItemsSource = result; } } }
On the XAML, we have put ListBox control do bind data from HTTP Service in the control.
If you are not aware of Data Binding in ListBox then watch a quick Video here
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="ASP.Net Web API" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox x:Name="lstBloggers"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}" /> <TextBlock Text="{Binding AreaOfIntrest}" Style="{StaticResource PhoneTextAccentStyle}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Grid>
Now press F5 to run the application. You should able to get Bloggers detail from ASP.Net Web API service in Windows Phone Application.
I hope you find this post useful. Thanks for reading.
Leave a Reply