ASP.NET Web API is a framework that makes it easy to build
HTTP services that reach a broad range of clients, including browsers and
mobile devices. ASP.NET Web API is an ideal platform for building RESTful
applications on the .NET Framework.
Note: ASP.Net Web API can also be used as a stand-alone Web services application.
Let us create a simple WebAPI and consume it in another
application. Create a New Project, and follow below steps:
In Models, create a Class (usually Data Model) and name it Customer.
Code
- public class Customer
- {
- public int CustomerId
- {
- get;
- set;
- }
- public string CustomerName
- {
- get;
- set;
- }
- public string Country
- {
- get;
- set;
- }
- }
Add a Controller and name it CustomerController.
Now let us assign Properties to Customer and return that
object.
Code
- using SampleWebAPI.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Web.Http;
- using System.Web.Mvc;
- namespace SampleWebAPI.Controllers
- {
- public class CustomerController: ApiController
- {
- public Customer GetCustomer()
- {
- Customer customer = new Customer
- {
- CustomerId = 101, CustomerName = "ABC", Country = "India"
- };
- return customer;
- }
- }
- }
When you run the above code you will be able to see a browser opening in that. Just go for that Web API controller like the following:
Create a New Project and add the following code.
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CustomerDetails
- {
- class Program
- {
- public class Customer
- {
- public int CustomerId
- {
- get;
- set;
- }
- public string CustomerName
- {
- get;
- set;
- }
- public string Country
- {
- get;
- set;
- }
- }
- static void Main(string[] args)
- {
- var url = "http://localhost:61453/api/Customer/GetCustomer";
- var t = _download_serialized_json_data < Customer > (url);
- Console.WriteLine("Customer Details:");
- Console.WriteLine("Customer ID :" + t.CustomerId.ToString());
- Console.WriteLine("Customer Name:" + t.CustomerName);
- Console.WriteLine("Customer Country:" + t.Country);
- Console.ReadLine();
- }
- private static T _download_serialized_json_data < T > (string url) where T: new() {
- using(var w = new System.Net.WebClient())
- {
- var json_data = string.Empty;
- // attempt to download JSON data as a string
- try {
- Console.WriteLine("Started downloading data");
- json_data = w.DownloadString(url);
- Console.WriteLine("Completed downloading");
- } catch (Exception) {}
- // if string with JSON data is not empty, deserialize it to class and return its instance
- return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();
- }
- }
- }
- }
I’ve added Newtonsoft.Json deserializer from Nuget. Now run, WebAPI application since it is not hosted in IIS
now, and in parallel run Console, you should be able to see the data.
Advantages of Web API
It supports convention-based CRUD Actions since it works with HTTP verbs GET, POST, PUT and DELETE. Can be Hosted in IIS or within application. Response can be Media Formattable to JSON or XML.

Adrian BordonesPosted Oct 20, 2024, 9:32 PM
Thanks a lot for this great example. Greetings from Panama.
SubashPosted Aug 21, 2016, 3:04 AM
Nice job
Sridhar SharmaPosted Dec 28, 2015, 8:25 AM
Thanks all :)
KaustubhPosted Dec 26, 2015, 7:04 AM
To call using POST request and passing Customer type as parameter modify method _download_serialized_json_data as follows: NameValueCollection myNameValueCollection = new NameValueCollection(); myNameValueCollection.Add("CustomerId", "1"); myNameValueCollection.Add("CustomerName", "kaustubh"); myNameValueCollection.Add("Country", "US"); byte[] arrResponse = w.UploadValues(url, myNameValueCollection); json_data = Encoding.ASCII.GetString(arrResponse);
Gowtham KPosted Dec 24, 2015, 1:20 PM
Good One
Santhakumar MunuswamyPosted Dec 24, 2015, 10:18 AM
Thanks for nice article
Ankit BansalPosted Dec 24, 2015, 7:39 AM
nice..
Manas MohapatraPosted Dec 24, 2015, 7:11 AM
Good One, Sri
Ranjit PowarPosted Dec 24, 2015, 7:01 AM
nice
Raja TPosted Dec 24, 2015, 6:19 AM
Nice, Thanks for sharing