This demo is created to get the basic understanding of WebAPI. We will be creating a RESTful Web API using Visual Studio 2017.
Before going through this, I assume you have a basic understanding of the following.
- What is WebAPI
- .NET Visual studio 2017
- ASP.NET Core
- Dapper for SQL queries
- Postman
The very first step to start the project is to create a table in the SQL database. You can use any databases like MySQL or SQL Express. For demo purposes, I am using SQL Server.
Use the below SQL script to create a table used in this demo.
- CREATE TABLE [dbo].[Employee](
- [EmployeeId] [int] IDENTITY(1,1) NOT NULL,
- [Name] [varchar](20) NOT NULL,
- [City] [varchar](20) ,
- [Department] [varchar](20),
- [JoinDate] date
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
Now, open VS 2017 and create a new project, as shown below.
Select the API (Project template for RESTFull HTTP service). Please uncheck HTTPS as we are not using it here for demo purposes. Also, you can select Docker support by selecting "Enable Docker support", however, we will install it later using NuGet Package Manager.
Click OK and VS will create a project template for you, as shown below.
Under Solution Explorer, go to Dependencies and install the following packages required for this project. Please note that we are not using the latest builds for these packages as it is creating compatibility issues in building the WebAPI using VS 2017 template.

Once completed, add a folder named "Models" under the Project folder and add a class Emp.cs.
Add the following lines of code to the Employee class.
- public class Emp
- {
- public int EmployeeId;
- public string EmployeeName;
- public string EmpCity;
- public string Department;
- }
Now, we have to add a data layer to get the data from the database. We already installed Dapper in our project. Dapper is having extension methods to fetch and store the data from the database. To read more about the package, please follow the link below.
Also, you can use ADO.NET or any other connection manager if you don’t want to use this method to connect to your database.
Here, we are creating a simple Interface and class to get the data from the database. To do so, add a folder “DataAccess” and add the following two classes to it.
- IDataProvider.cs : Interface
- EmpDataAccess.cs: Data Access

- Task<IEnumerable<Emp>> GetAllEmployees();
- Task<Emp> GetEmployee(int empid);
To provide the features to get all employee and to search employee using employee id, we have to add the following code for EmpDataAccess class.
Make sure to use the following references for EmpDataAccess class.
- using Employee.WebAPI.Models;
- using System.Data.SqlClient;
- using System.Data;
- using Dapper;
- public class EmpDataAccess: IDataProvider
- {
- private readonly string connectionString = "your connection";
- public async Task<Emp> GetEmployee(int Id)
- {
- string _sql = "select * from Employee where EmployeeID =1=@eid order by dt_updated desc";
- using (var sqlConnection = new SqlConnection(connectionString))
- {
- await sqlConnection.OpenAsync();
- var dynamicParameters = new DynamicParameters();
- dynamicParameters.Add("@eid", Id);
- return await sqlConnection.QuerySingleOrDefaultAsync<Emp>(
- _sql,
- dynamicParameters,
- commandType: CommandType.Text);
- }
- }
- public async Task<IEnumerable<Emp>> GetAllEmployees()
- {
- string _sql = "select * from Employee";
- using (var sqlConnection = new SqlConnection(connectionString))
- {
- await sqlConnection.OpenAsync();
- return await sqlConnection.QueryAsync<Emp>(
- _sql,
- null,
- commandType: CommandType.Text);
- }
- }
- }
Join the conversation! Your thoughts help the community grow.