Introduction
ASP.NET Web API and ASP.NET MVC previously had separate types and setups, but the Web API from ASP.NET Core is the same as the one from ASP.NET Core MVC. The Web API offers a simple communication way based on Representational State Transfer (REST). With REST, HTTP verbs such as GET, POST, PUT, and DELETE is used. GET is used to retrieve resources, POST is used to add new resources, PUT is used to update resources, and DELETE is used to delete resources.
Building Services
Using .NET Core, you need to start with an ASP.NET Core Web Application and select the Web API in the following dialog. This template adds folders and references needed with the Web API. The directory structure that is created with this template contains folders that are needed for creating the services. The Controllers directory contains the Web API controllers, Web API, and ASP.NET Core MVC makes use of the same infrastructure.

Section 1
Create a PlaceInfo datatable in MSSQL. Also, it creates a class PlaceInfo in the model folder.
PlaceInfo Table Structure
CREATE TABLE [dbo].[PlaceInfo](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Place] [varchar](50) NOT NULL,
[About] [varchar](300) NOT NULL,
[City] [varchar](20) NOT NULL,
[State] [varchar](20) NOT NULL,
[Country] [varchar](20) NOT NULL,
CONSTRAINT [PK_PlaceInfo] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
PlaceInfo Class
public class PlaceInfo
{
public int Id { get; set; }
[Required]
public string Place { get; set; }
[Required]
public string About { get; set; }
[Required]
public string City { get; set; }
[Required]
public string State { get; set; }
[Required]
public string Country { get; set; }
}
Create a Services folder in project and add two classes, one is for interface IPlaceInfoService and add method in interface Add(),AddRange(),GetAll(),Find(),Remove() & Update(). Second create PlaceInfoService class is derived from IPlaceInfoService interface. This defines the body of all interface methods, Add(), AddRange(), GetAll(), Find(), Remove() & Update() in PlaceInfoService class.
public interface IPlaceInfoService
{
int Add(PlaceInfo placeInfo);
int AddRange(IEnumerable<PlaceInfo> places);
IEnumerable<PlaceInfo> GetAll();
PlaceInfo Find(int id);
int Remove(int id);
int Update(PlaceInfo placeInfo);
}
public class PlaceInfoService : IPlaceInfoService
{
public int Add(PlaceInfo placeInfo)
{
string sQry = "INSERT INTO [PlaceInfo] ([Place],[About],[City],[State],[Country]) " +
"VALUES('" + placeInfo.Place + "','" + placeInfo.About + "','" + placeInfo.City + "','" +
placeInfo.State + "','" + placeInfo.Country + "')";
int retVal=ExecuteCRUDByQuery(sQry);
return retVal;
}
public int AddRange(IEnumerable<PlaceInfo> places)
{
string sQry = "INSERT INTO [PlaceInfo] ([Place],[About],[City],[State],[Country]) VALUES";
string sVal = "";
foreach(var place in places)
sVal+= "('" + place.Place + "','" + place.About + "','" + place.City + "','" + place.State + "','" + place.Country + "'),";
sVal = sVal.TrimEnd(',');
sQry = sQry + sVal;
int retVal=ExecuteCRUDByQuery(sQry);
return retVal;
}
public PlaceInfo Find(int id)
{
PlaceInfo placeInfo = null;
string sQry = "SELECT * FROM [PlaceInfo] WHERE [Id]="+id;
DataTable dtPlaceInfo = ExecuteQuery(sQry);
if (dtPlaceInfo != null)
{
DataRow dr = dtPlaceInfo.Rows[0];
placeInfo = GetPlaceInfoByRow(dr);
}
return placeInfo;
}
public IEnumerable<PlaceInfo> GetAll()
{
List<PlaceInfo> placeInfos = null;
string sQry = "SELECT * FROM [PlaceInfo]";
DataTable dtPlaceInfo = ExecuteQuery(sQry);
if (dtPlaceInfo != null)
{
placeInfos = new List<PlaceInfo>();
foreach (DataRow dr in dtPlaceInfo.Rows)
placeInfos.Add(GetPlaceInfoByRow(dr));
}
return placeInfos;
}
public int Remove(int id)
{
string sQry = "DELETE FROM [PlaceInfo] WHERE [Id]=" + id;
int retVal=ExecuteCRUDByQuery(sQry);
return retVal;
}
public int Update(PlaceInfo placeInfo)
{
string sQry = "UPDATE [PlaceInfo] SET [Place]='" + placeInfo.Place + "',[About]='" + placeInfo.About + "',[City]='" + placeInfo.City + "',[State]='" + placeInfo.State + "',[Country]='" + placeInfo.Country + "' WHERE [Id]=" + placeInfo.Id;
int retVal=ExecuteCRUDByQuery(sQry);
return retVal;
}
// code detail is below
private int ExecuteCRUDByQuery(string strSql){ }
private DataTable ExecuteQuery(string strSql){ }
private PlaceInfo GetPlaceInfoByRow(DataRow dr){ }
}
Created three private functions which are named ExecuteCRUDByQuery,ExecuteQuery and GetPlaceInfoByRow. All three functions are used to communicate with theSQL database server. The first function ExecuteCRUDByQuery is used to insert, update and delete record in datatable by passing SQL query. The second function ExecuteQuery is used to get records from datatable by passing sql query. Function GetPlaceInfoByRow is used to get records from datarow to PlaceInfo model class.



sayyad hasanPosted Jun 21, 2020, 4:34 PM
Nice explanation
Santhosh PatilPosted May 6, 2020, 2:15 PM
Very to use swagger , but code looks too much glimpse and complexity ...
Rikam PalkarPosted May 4, 2020, 12:05 PM
Nice one article!! Keep contributing..