An Overview Of Web API

What is Web API


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

  1. public class Customer  
  2. {  
  3.     public int CustomerId  
  4.   {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string CustomerName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string Country  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  

Add a Controller and name it CustomerController.




Now let us assign Properties to Customer and return that object.

Code

  1. using SampleWebAPI.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Net.Http.Headers;  
  8. using System.Text;  
  9. using System.Web.Http;  
  10. using System.Web.Mvc;  
  11.   
  12. namespace SampleWebAPI.Controllers  
  13. {  
  14.     public class CustomerController: ApiController  
  15.     {  
  16.         public Customer GetCustomer()   
  17.         {  
  18.             Customer customer = new Customer   
  19.             {  
  20.                 CustomerId = 101, CustomerName = "ABC", Country = "India"  
  21.             };  
  22.             return customer;  
  23.         }  
  24.     }  
  25. }  

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:

Here you will be able to see a JSON file downloading. We will consume this in another application and will make use of it. Now let us create a new Console appication and try to access this service. 

Create a New Project and add the following code.  

  1. using Newtonsoft.Json;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace CustomerDetails  
  9. {  
  10.     class Program   
  11.     {  
  12.         public class Customer   
  13.       {  
  14.             public int CustomerId   
  15.             {  
  16.                 get;  
  17.                 set;  
  18.             }  
  19.             public string CustomerName  
  20.             {  
  21.                 get;  
  22.                 set;  
  23.             }  
  24.             public string Country  
  25.             {  
  26.                 get;  
  27.                 set;  
  28.             }  
  29.         }  
  30.         static void Main(string[] args)   
  31.         {  
  32.             var url = "http://localhost:61453/api/Customer/GetCustomer";  
  33.             var t = _download_serialized_json_data < Customer > (url);  
  34.             Console.WriteLine("Customer Details:");  
  35.             Console.WriteLine("Customer ID :" + t.CustomerId.ToString());  
  36.             Console.WriteLine("Customer Name:" + t.CustomerName);  
  37.             Console.WriteLine("Customer Country:" + t.Country);  
  38.             Console.ReadLine();  
  39.   
  40.         }  
  41.         private static T _download_serialized_json_data < T > (string url) where T: new() {  
  42.             using(var w = new System.Net.WebClient())  
  43.             {  
  44.                 var json_data = string.Empty;  
  45.                 // attempt to download JSON data as a string  
  46.                 try {  
  47.                     Console.WriteLine("Started downloading data");  
  48.                     json_data = w.DownloadString(url);  
  49.                     Console.WriteLine("Completed downloading");  
  50.                 } catch (Exception) {}  
  51.                 // if string with JSON data is not empty, deserialize it to class and return its instance  
  52.                 return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();  
  53.             }  
  54.         }  
  55.     }  
  56. }  

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.

Up Next
    Ebook Download
    View all
    Learn
    View all