What is Web API and How to Create Web API Project in Visual Studio

Introduction

 
What is Web API?  The term API stands for 'Application Programming Interface'. Web API is a framework for building HTTP services that can be consumed by a broad range of clients including browsers, mobiles, iPhones, and tablets.
 
Steps 
 
Follow the below steps to create a Web API project in Visual Studio
 
I have used Visual Studio 2015 Express Web and PostgreSQL to develop Web API application.
 
Let's start with creating a Web API project in visual studio.
 
Open visual studio -> Go to File -> Select New à Select Project
 
Step 1 - In the template pane
  1. Select Installed Templates
  2. Expand the Visual C# menu. 
  3. Inside that Visual C# select Web.
  4. In the list of projects select ASP.Net MVC 4 Web Application.
And name the project WebAPI_Demo.
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 2
 
After clicking on OK, one more window will appear. Check on MVC and Web API checkbox and click on OK, as shown in the below screenshot.
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 3
 
So, finally Web API - MVC project is created. It will create a default structure for the project.
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 4
 
Set the home page URL in RouteConfig class file as shown in below snapm in any MVC url contains the 3 values given values.
  1. Controller Name (in url controller name is a must. It is the first parameter of url).
  2. Action Name (in url action name is a must. It is the second parameter of url).
  3. Id (individual - it is mandatory)
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 5
 
Check out the WebApiConfig.cs to find out Web API routes or URL path of WEB API to get records in XML format 
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 6
 
Below snap is the webconfig file which is used for the connection string with PostgreSQL 
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 7
 
PostgreSQL to create a table and execute the query.
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 8
 
Create a new controller called CarDetailsController as shown below snap. 
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 9
 
Call the API using JSON datatype. Below snap can be used for reference.
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 10
 
In the SQL helper file write all the operation code.
 
What Is Web API And How To Create Web API Project In Visual Studio
 
Step 11
 
Output of the execute code with below URL
 
http://localhost:50698/api/Cardetails
 
What Is Web API And How To Create Web API Project In Visual Studio
 

Summary

 
I hope you like this article. We have created a WEB API Project in Visual Studio. This article will help all beginners who want to start implementing a Web API.


Recommended Free Ebook
Similar Articles