Creating your first WEB API Project

Introduction

Whenever we talk about WEB API the very first question come in our mind is what actually is API. API stand for “Application Programming Interface”. From Wikipedia definition – A Web API is an application programming interface for either a web server or a web browser. It is a web development concept, usually limited to a web application’s client-side (including any web frameworks being used), and thus usually does not include web server or browser implementation details such as SAPIs(Server Application Programming Interface) or APIs(Application Programming Interface) unless publicly accessible by a remote web application. In short with the help of WEB API we can create interface which is easily accessible for other software to interact or communicate with it on any platform either it is web, windows or mobile based platform. If you are novice and do not know any thing related to WEB API, don’t worry! We are here to guide you. In this article you will learn how to “Creating your first WEB API Project”.

Creating your first WEB API Project

  • Open Visual Studio 2013/15/17 either it is Community version, professional version or enterprise version.
  • Click on File Menu
  • Click on New and then Click on Project menu.
  • This will open new window – New project, where you can select project type.
  • Expand Installed, Expand Language and select web.
  • Select .Net framework on which you are going to build your WEB API.
  • Select Template. Here I’ve selected ASP.NET Web Application(.Net Framework)
  • Enter Project Name. Here I’ve entered CreateWEBAPI
  • Click on OK button.

 

Just after clicking on OK button, A window(New Asp.Net Web Application – CreateWEBAPI) will appear. Here follow given steps.

  • Select Empty Template
  • Select WEB API
  • Click on OK button.

 

Creating your first WEB API Project

You are done with preparing environment to write code for your first WEB API. Now create a modal. To create a Model follow given steps.

  • Right click on Models
  • Click on Add
  • Click on Class and create a class name Student

 

Once you create a model name “Student”. Write below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CreateWEBAPI.Models
{
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
        public char Section { get; set; }
    }
}

Now you have to create Controller where you can write action method in order to expose your data so client can consume it. To create a controller follow given steps.

  • Expand Installed
  • Click on Controller
  • Select Controller template. Here we are using Web API 2 Controller – Empty
  • Click on OK button.

The Controller class plays an important role, because requests coming from the client hits the controller first. Then the controller decides which model to use to serve the incoming request.

In order to make the sample simple, we will load the model with data inside this controller instead of loading it from a database. The following is the code for the controller class.

using System.Collections.Generic;
using System.Web.Http;
using CreateWEBAPI.Models;

namespace CreateWEBAPI.Controllers
{
    public class StudentController : ApiController
    {
        Student[] students = new Student[]
          {
            new Student{ID = 1, Class = "Matriculation", Name ="Ravi", Section = 'A'},
              new Student{ID = 2, Class = "Intermediate", Name ="Ram", Section = 'B'},
                new Student{ID = 3, Class = "Graduation", Name ="Shyam", Section = 'A'},,
          };
        public IEnumerable<Student> Get()
        {
            return students;
        }
    }
}

Now, We’ve  successfully created WEB API and  it’s time to test  HTTP service using the ASP.NET MVC Web API. Press “F5” to run the application.

In order to call our Student controller
change the URL to “http://localhost:XXXX/api/Student”. You will see the results as shown in the following output window.

 

Hopefully, this simple article will be helpful you to write code for your first HTTP service using the ASP.NET Web API. Further, we’ll learn another Web API HTTP service with all Create, Read, Update, and Delete (CRUD) operations in the coming days till then stay tuned. If you have any query while understanding and implementing please don’t forget to write to us.

You can also read :
How to secure ASP.NET Web API using basic authentication
How to secure ASP.NET Web API using Token Based Authentication

Share Knowledge

One thought on “Creating your first WEB API Project”

  1. Pingback: Implementing CRUD Operations using WEB API - code-adda
  2. Trackback: Implementing CRUD Operations using WEB API - code-adda

Leave a Reply