Introduction to this tutorial
- Create Database APICoreDB
- Use APICoreDB
- Create table Employee(Id int primary key identity(1,1), Name varchar(50), Department varchar(20), Salary decimal(10,2))
- Insert into Employee(Name, Department, Salary)Values('John', 'DotNet', 50000)
- Select * from Employee
- Open Visual Studio
- Select Create a new project
- Select "ASP.NET Core Web Application (C#)" from the left panel
- Select "ASP.NET Core Web Application" from the right-side panel
- Now Click on the "Next" button 6. Give the Project name as "WebAPICore"
- Select the location or leave it as it is
- Now click on the "Create" button
- {
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*",
- "DbConnection": "Server=DESKTOP-TA2C85E\\SQLEXPRESS;Database=APICoreDB;User ID=sa;Password=123456;"
- }
- services.AddHttpClient();
- services.AddDbContext<APICoreDBContext>(options =>
- options.UseSqlServer(Configuration["DbConnection"]));
- services.AddTransient<IEmployeeService, EmployeeService>();

- IEnumerable<Employee> GetEmployee();
- Employee GetEmployeeById(int id);
- Employee AddEmployee(Employee employee);
- Employee UpdateEmployee(Employee employee);
- Employee DeleteEmployee(int id);

Now implement the interface method inside the file "EmployeeService.cs"
- You need to inherit interface then
- Create an instance of the DBContext class using the constructor
- APICoreDBContext dbContext;
- public EmployeeService(APICoreDBContext _db)
- {
- dbContext = _db;
- }
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using WebAPICore.IServices;
- using WebAPICore.Models;
- namespace WebAPICore.Services {
- public class EmployeeService: IEmployeeService {
- APICoreDBContext dbContext;
- public EmployeeService(APICoreDBContext _db) {
- dbContext = _db;
- }
- public IEnumerable < Employee > GetEmployee() {
- var employee = dbContext.Employee.ToList();
- return employee;
- }
- public Employee AddEmployee(Employee employee) {
- if (employee != null) {
- dbContext.Employee.Add(employee);
- dbContext.SaveChanges();
- return employee;
- }
- return null;
- }
- public Employee UpdateEmployee(Employee employee) {
- dbContext.Entry(employee).State = EntityState.Modified;
- dbContext.SaveChanges();
- return employee;
- }
- public Employee DeleteEmployee(int id) {
- var employee = dbContext.Employee.FirstOrDefault(x => x.Id == id);
- dbContext.Entry(employee).State = EntityState.Deleted;
- dbContext.SaveChanges();
- return employee;
- }
- public Employee GetEmployeeById(int id) {
- var employee = dbContext.Employee.FirstOrDefault(x => x.Id == id);
- return employee;
- }
- }
- }
- Choose "Add" Option then
- Choose "Controller..." then
- Select "API Controller - Empty" from the listed item then
- Click on the "Add" button then
- Provide the name to the controller as "EmployeeController.cs" and then
- Click on the "Add" button
- private readonly IEmployeeService employeeService;
- public EmployeeController(IEmployeeService employee)
- {
- employeeService = employee;
- }
- using System.Collections.Generic;
- using Microsoft.AspNetCore.Mvc;
- using WebAPICore.IServices;
- using WebAPICore.Models;
- namespace WebAPICore.Controllers {
- [Route("api/[controller]")]
- [ApiController]
- public class EmployeeController: ControllerBase {
- private readonly IEmployeeService employeeService;
- public EmployeeController(IEmployeeService employee) {
- employeeService = employee;
- }
- [HttpGet]
- [Route("[action]")]
- [Route("api/Employee/GetEmployee")]
- public IEnumerable < Employee > GetEmployee() {
- return employeeService.GetEmployee();
- }
- [HttpPost]
- [Route("[action]")]
- [Route("api/Employee/AddEmployee")]
- public Employee AddEmployee(Employee employee) {
- return employeeService.AddEmployee(employee);
- }
- [HttpPut]
- [Route("[action]")]
- [Route("api/Employee/EditEmployee")]
- public Employee EditEmployee(Employee employee) {
- return employeeService.UpdateEmployee(employee);
- }
- [HttpDelete]
- [Route("[action]")]
- [Route("api/Employee/DeleteEmployee")]
- public Employee DeleteEmployee(int id) {
- return employeeService.DeleteEmployee(id);
- }
- [HttpGet]
- [Route("[action]")]
- [Route("api/Employee/GetEmployeeId")]
- public Employee GetEmployeeId(int id) {
- return employeeService.GetEmployeeById(id);
- }
- }
- }

That's it for this tutorial. I Have covered the How to create CORE API projects using Visual Studio 2019 and .Net Core Version 3.1.

hossein eskandaryPosted Jan 11, 2021, 11:15 AM
Hi The question I have is how can I now give this Asp.net core api project to a .net framework console project when I get a User and Pass inside the API? Is it possible to do this at all? Then the console project takes these values and connects to Dynamic crm and performs the CRUD operation and returns the READ result to the API as a Response?
Bhuvanesh MohankumarPosted Oct 8, 2020, 10:03 AM
Good one, Why you selected Entity framework over ADO.Net, any specific reason [As EF has delay while handling huge data], also what Authentication is implemented in API service access like Oauth or JWT. Someone can suggest the best Authentication.
Mukesh SagarPosted Sep 1, 2020, 11:20 AM
Nice tutorial, pl add the sample project code also that much more beneficial.
lsj lsjPosted Aug 4, 2020, 8:58 PM
Very informative and Great tutorial
Rohit RaoPosted Aug 3, 2020, 2:35 PM
This article looks good for a beginner. It would be good if you can add a files/folder structure of your project.
Khaja MoizuddinPosted Aug 2, 2020, 1:59 PM
Very interesting article Gautham, looking forward to learn more from you....
Hamid KhanPosted Jul 31, 2020, 10:36 AM
Thanks for sharing..........................
Hamid KhanPosted Jul 31, 2020, 10:36 AM
Good start,...............Keep it up................