How to Consume ASP.Net Web API

Introduction

This article explains how to consume in the ASP.NET Web API. The Web API is defined as Consumption by various applications for supporting data transfer. It is also used for creating HTTP services.

Step 1

Create a Web API application as in the following:

  • Start Visual Studio 2012.
  • From the start window Select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click on the "Ok" button.

    Select MVC4 Application

  • From the "MVC4 Project" window select "Web API".

    Select Web API

  • Click on the "OK" button.

Step 2

Creating a model class:

  • In the "Solution Explorer".
  • Right-click on the Model Folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select class.

    Add Model Class

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace ConsumeAPI.Models  
  6. {  
  7.     public class Vehical  
  8.     {  
  9.         public int Vehical_ID { getset; }  
  10.         public string Va_Cmp { getset; }  
  11.         public string V_Colr { getset; }  
  12.     }  
  13. } 

Step 3

Add an apicontroller as in the following:

  • In the "Solution Explorer".
  • Right-click on the Controller folder.
  • Select "Add" -> "Controller".

    Add API Controller

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using ConsumeAPI.Models;  
  8. namespace ConsumeAPI.Controllers  
  9. {  
  10.     public class VehicalController : ApiController  
  11.     {  
  12.         List<Vehical> model = new List<Vehical>()  
  13.         {  
  14.            new Vehical()  
  15.            {  
  16.                Vehical_ID = 101,  
  17.                Va_Cmp = "Tvera",  
  18.                V_Colr = "White",  
  19.            },  
  20.            new Vehical()  
  21.            {   
  22.                Vehical_ID = 102,  
  23.                Va_Cmp = "Toyota",  
  24.                V_Colr = "Gray",  
  25.            },  
  26.            new Vehical()  
  27.            {  
  28.                Vehical_ID = 1002,  
  29.                Va_Cmp = "BMW",  
  30.                V_Colr = "Red",  
  31.            },  
  32.            new Vehical()  
  33.            {  
  34.                Vehical_ID = 1002,  
  35.                Va_Cmp = "Hyundai",  
  36.                V_Colr = "Black",  
  37.            }  
  38.         };  
  39.         public IEnumerable<Vehical> GetAllVehicals()  
  40.         {  
  41.             return model;  
  42.         }  
  43.         public Vehical GetVehicalById(int id)  
  44.         {  
  45.             var car = model.FirstOrDefault((I) => I.Vehical_ID == id);  
  46.             if (car == null)  
  47.             {  
  48.                 throw new HttpResponseException(HttpStatusCode.NotFound);  
  49.             }  
  50.           return car;  
  51.         }   
  52.     }  
  53. }   

Here we create a "VehicalController" class that inherits from the "apicontroller" class. In it the action method returns data, not views. The action method is mapped to the GET, PUT, POST, DELETE HTTP operations.

Step 4

Execute the application. The output will be:

Get All Vehical Name

Get by ID:

Get Vehical By ID