Learn About ASP.Net Web API 2

Introduction

This article explains the Web API 2. Here I just explain the Web API 2 Controller, Model class and show how to run this application on Fiddler. We know that the Web API 2 is the second version of the Web API. So the Web API 2 contains all the features of the Web API including some new features such as:

  • Attribute Routing.
  • IHttpActionResult.
  • Web API OData.
  • Cors- Cross Origin Resource Sharing.
  • OWIN

Now we see how to create an application using Web API 2 and run it using Fiddler tool.

  1. How we create an application using Visual Studio 2013.
    • Start Visual Studio 2013.
    • From the Start Window select "New Project".
    • Select "Installed" -> "Templates" -> "Visual C#" -> "Web" and select ASP.NET Web Application.

      Slect Web Application

    • Click on the "OK" button.

      Select Empty Template with Web API

    • From the ASP.Net project window select "Empty" and select the "Web API" check box.
    • Click on the "Create Project" button.
  2. Use the following procedure to install the necessary package from the NuGet Package Manager:
    • Go To the "Tools" Menu.
    • Select "Library Package Manager".
    • Then select "Manage NuGet Package for Solution".

      Install web API2 package

    • In the search box type "Web API" and install the necessary package.
  3. Use the following procedure to create 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 "OK" button.

      Add the following code:
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Web;  
      5. namespace WebApplication2.Models  
      6. {  
      7.     public class Detail  
      8.     {  
      9.         public int ID { getset; }  
      10.         public string Name { getset; }  
      11.         public string Address { getset; }  
      12.     }  
      13. }  
  4. Use the following procedure to create a Web API 2 Controller:
    • In the "Solution Explorer".

    • Right-click on the "Controller" folder.

    • Select the "Controller" and from the controller window select "Common" -> "Web API".
      Add Web API2 Controller
    • Select the "Web API 2 Empty Controller" and click on the "Add" button.

      Name of Controller

    • Change the name and 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 WebApplication2.Models;  
      8. namespace WebApplication2.Controllers  
      9. {  
      10.     public class DetailsController : ApiController  
      11.     {  
      12.         Detail[] details = new Detail[]  
      13.         {  
      14.             new Detail{ID=1, Name="Employee1", Address="Address1"},  
      15.               new Detail{ID=2, Name="Employee2", Address="Address2"},  
      16.                 new Detail{ID=3, Name="Employee3", Address="Address3"},  
      17.                   new Detail{ID=4, Name="Employee4", Address="Address4"},  
      18.                     new Detail{ID=5, Name="Employee5", Address="Address5"},  
      19.                       new Detail{ID=6, Name="Employee6", Address="Address6"}  
      20.         };  
      21.         public IEnumerable<Detail> GetAllDetail()  
      22.         {  
      23.             return details;  
      24.         }  
      25.         public IHttpActionResult GetDetailByID(int id)  
      26.         {  
      27.             var detail = details.FirstOrDefault((p) => p.ID == id);  
      28.             if(detail==null)  
      29.             {  
      30.                 return NotFound();  
      31.             }  
      32.             return Ok(detail);  
      33.         }  
      34.     }  
      35. }  
  5. Execute the application and copy the URL.
  6. Open Fiddler and cllick on the "Composer" tab and paste the URL. The URL is "http://localhost:12939/api/details". Click on the "Execute" button; it will then display all the details.

    Get all Details
  7. For finding the details by ID change the URL to http://localhost:12939/api/details/5. It displays the details of Id 5.

    Get detail by ID


Similar Articles