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.
  1. 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.
  1. 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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace WebApplication2.Models

{

    public class Detail

    {

        public int ID { getset; }

        public string Name { getset; }

        public string Address { getset; }

    }

}

  1. 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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using WebApplication2.Models;

namespace WebApplication2.Controllers

{

    public class DetailsController : ApiController

    {

        Detail[] details = new Detail[]

        {

            new Detail{ID=1, Name="Employee1", Address="Address1"},

              new Detail{ID=2, Name="Employee2", Address="Address2"},

                new Detail{ID=3, Name="Employee3", Address="Address3"},

                  new Detail{ID=4, Name="Employee4", Address="Address4"},

                    new Detail{ID=5, Name="Employee5", Address="Address5"},

                      new Detail{ID=6, Name="Employee6", Address="Address6"}

        };

        public IEnumerable<Detail> GetAllDetail()

        {

            return details;

        }

        public IHttpActionResult GetDetailByID(int id)

        {

            var detail = details.FirstOrDefault((p) => p.ID == id);

            if(detail==null)

            {

                return NotFound();

            }

            return Ok(detail);

        }

    }

}

 

  1. Execute the application and copy the URL.
  2. 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

  1. 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

Up Next
    Ebook Download
    View all
    Learn
    View all