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
- Select Installed Templates
- Expand the Visual C# menu.
- Inside that Visual C# select Web.
- In the list of projects select ASP.Net MVC 4 Web Application.
And name the project WebAPI_Demo.
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.
Step 3
So, finally Web API - MVC project is created. It will create a default structure for the project.
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.
- Controller Name (in url controller name is a must. It is the first parameter of url).
- Action Name (in url action name is a must. It is the second parameter of url).
- Id (individual - it is mandatory)
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
Step 6
Below snap is the webconfig file which is used for the connection string with PostgreSQL
Step 7
PostgreSQL to create a table and execute the query.
Step 8
Create a new controller called CarDetailsController as shown below snap.
Step 9
Call the API using JSON datatype. Below snap can be used for reference.
Step 10
In the SQL helper file write all the operation code.
Step 11
Output of the execute code with below URL
http://localhost:50698/api/Cardetails
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.