Archive for the ‘WebAPI’ Category
Asp.Net WebAPI is a framework for building RESTful HTTP services which can be used across a wide range of clients including web, phone and desktop applications. With WebAPI we can use XML and JSON to send and retrieve data from the service. The use of Json or XML makes it quite flexible to be used across a variety of devices.
Although WebAPI ships and installs with ASP.Net MVC 4, it can be used in normal Web Forms application as well.
In this example, we will use WebAPI with a traditional Web Forms application for adding and displaying a list of User Information. We will be using Visual Studio 2012 and .Net 4.5.
Creating the Project
Open Visual Studio 2012, Go To New -> Project. Select Visual C# -> Web from the left hand navigation and select project type as ASP.Net Web Forms Application. Enter the project name as WebAPIDemo
A new project will be created with a default template and our solution will show the following structure
Adding Model Class
We will then add a Model Class to our project. The Model class will represent the data in our application. We will add a model class called Users in our project
In this post, we are going to learn how to call an ASP.Net WebAPI using HttpClient libraries. The HttpClient Library is quite useful and can be used while calling your WebAPI from Windows applications, Console Applications or even Windows 8 applications.
We will use the same WebAPI which we created in my previous post “Using ASP.Net WebAPI with WebForms” but this time we will consume it from a WPF application.
Create A WPF Application Project
Open Visual Studio 2012, Go To New -> Project. Select Visual C# -> Windows from the left hand navigation and select project type as WPF Application. Enter the project name as WebAPIClient.
Installing WebAPI Client Libraries
We will then add the HttpClient libraries in our project using the NuGet Package Manager which makes it very easy to install the libraries in our project
In the Tools menu, select Library Package Manager -> Manage NuGet Packages For Solution
In the Manage NuGet Packages dialog box which opens, select online and search for “Http Client” and select the Microsoft Http Client Libraries which appear as shown below
Select Install and it will ask you to select the project in which you want to install it. Click on Ok and close the dialog once the installation is done.
Similarly also install the package “JSON.Net” from Nuget Package Manager.