Learn Web API Using WPF, WebForms And Xamarin

What is ASP.NET Web API?

ASP.NET Web API is a framework to build Web API’s on the top of .NET framework, which makes it easy to build HTTP Services that comprises of range of clients, including mobile devices, all the Browsers & desktop Application.

Web API is a similar to ASP.NET MVC, so it contains all MVC features.

  • Model
  • Control
  • Routing
  • Model binding
  • Filter
  • Dependency injections

HTTP is not just for serving Web pages. It is also a powerful platform to build RESTful (Representational state transfer) API’s that expose Services and data. HTTP is simple, flexible & ubiquitous.

Why to use ASP.NET Web API?

Web API can be used anywhere in a wide range of client applications (Window & Web), Mobile device &  Browsers. Web API perfectly supports HTTP verbs (GET, POST, PUT, DELETE).


Create simple Web API to send a mail

Open Visual studio 2015. Go to New Project-> Select Visual C# -> Web & choose the project type as ASP.NET Web Application in the popup.


From the pop up given below, we will select the template as Web API.


Once the project is created, add new API in the controllers folder.->Right click on controllers-> add controller. Now, add Scaffold & Create as API Controller “SendMailApiController” will appear.


Using the namespace

Write Web.Config File

I am using Gmail domain & configuring from Mail ID in the Web.config file.

Once you run the Application, Web API REST Services are ready to consume.


To call Web API method from

  • WPF (Native Application)
  • WebForm (Web Application)
  • Xamarin (Mobile Application)Xamarin
Consume Web API in WPF(Native Application)

To create WPF Application ->New Project ->select Windows & choose WPF Application.


Simply design the Windows, as shown below, using XAML code (e.x) getting parameters to Email id, Email subject & Email body.


XAML code

Using the namespace given above

Base code

After inserting the namespace, create new instance of the HttpClient, followed by setting its base URL to http://localhost:51537/api

This accepts header property instruction, which is sent to the Server in response of JSON format.

new MediaTypeWithQualityHeaderValue(“application/json”)

The URL will be set in App.config file

Follow the code given above, copy & paste the button click event.

Enter any input in WPF Window & click Send Mail button.


Consume Web API in WebForms (Web Application)

To create Web application, go to New Project ->Visual C#->Select ASP.NET Web Application in this popup and choose the Web Forms templates.


Once the project is created, you can create new Web Forms and design the forms, as shown below.


Same WPF code style will follow to the Web Application. If there is no HttpClient, then get into NuGet Package Manager.


After installing the interface you can alter the WPF code for web Forms..

Now, run the Web Application and the mail will be received from what mail id is configured in Web API.


Consume Web API in Xamarin (Mobile Application)

To create Mobile application, go to New Project ->Visual C#->select Cross-Platform & choose Blank App, using Portable class library in the popup.


Right click on App (Portable) & add new Item->Forms Xaml Page.


Navigate my page in App.xaml.cs file

Just write XML code for an Android mobile UI, as shown below.

Run the mobile Application. This page appears in mobile device or an app will open an Android Emulator Manager.


Follow the same HttpClient settings & alter the code based on mobile UI in the button clicked event.

After click button, the mail is sent specified to the Email id.


Finally, we have used ASP.NET Web API in different types of Application.



In this article, we have learned Web API, using WPF, WebForms and Xamarin. If you have any queries, please tell me through the comments section.

Happy Coding!

Thiruppathi Rengasamy
I'm a Dotnet Developer & Blogger. And also I'm working as a senior software engineer. I would like to spend my precious time writing articles, research on .Net technologies, and learning new technologies.