Creating your first WEB API Project

Introduction

Whenever we talk about WEB API the very first question comes in our mind is what actually is API. API stands 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 an 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 a novice and do not know anything 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 a new window – New project, where you can select the 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 the 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

Now you have to create Controller where you can write action method in order to expose your data so the 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.

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 for 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

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