Introduction
In this article, we are going to create a product management web API with CRUD operations using .NET Core 6 and different forms with the help of React JS.
Prerequisites
- Visual Studio 2022
- VS Code
- SQL Server
- .NET Core SDK
- Node JS
Product Management Application
Step 1. Create a new Product Management .NET Core Web API.
Step 2. Install the following NuGet packages that we used for database migrations and connectivity with SQL Server.

Step 3. Add the product class inside the entities folder.
namespace ProductManagementAPI.Entities
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
Step 4. Create an AppDbContext class inside the data folder with a SQL Server connection and a DB set property.
using Microsoft.EntityFrameworkCore;
using ProductManagementAPI.Entities;
namespace ProductManagementAPI.Data
{
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected readonly IConfiguration Configuration;
public AppDbContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}
}
}
Step 5. Add a product repository inside the repositories folder.
IProductRepository
using ProductManagementAPI.Entities;
namespace ProductManagementAPI.Repositories
{
public interface IProductRepository
{
void AddProduct(Product product);
void DeleteProduct(int id);
List<Product> GetAllProducts();
Product GetProductById(int id);
void UpdateProduct(Product product);
}
}
ProductRepository
using Microsoft.EntityFrameworkCore;
using ProductManagementAPI.Data;
using ProductManagementAPI.Entities;
namespace ProductManagementAPI.Repositories
{
public class ProductRepository : IProductRepository
{
private readonly AppDbContext _context;
public ProductRepository(AppDbContext context)
{
_context = context;
}
public List<Product> GetAllProducts()
{
return _context.Products.ToList();
}
public Product GetProductById(int id)
{
return _context.Products.FirstOrDefault(p => p.Id == id);
}
public void AddProduct(Product product)
{
if (product == null)
{
throw new ArgumentNullException(nameof(product));
}
_context.Products.Add(product);
_context.SaveChanges();
}
public void UpdateProduct(Product product)
{
if (product == null)
{
throw new ArgumentNullException(nameof(product));
}
_context.Entry(product).State = EntityState.Modified;
_context.SaveChanges();
}
public void DeleteProduct(int id)
{
var product = _context.Products.Find(id);
if (product == null)
{
throw new ArgumentNullException(nameof(product));
}
_context.Products.Remove(product);
_context.SaveChanges();
}
}
}
Step 6. Create a new product controller with different action methods that we used to perform different operations using our front-end application after invoking the same.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductManagementAPI.Entities;
using ProductManagementAPI.Repositories;
namespace ProductManagementAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ProductController : ControllerBase
{
private readonly IProductRepository _productRepository;
public ProductController(IProductRepository productRepository)
{
_productRepository = productRepository;
}
[HttpGet]
public IActionResult GetAllProducts()
{
var products = _productRepository.GetAllProducts();
return Ok(products);
}
[HttpGet("{id}")]
public IActionResult GetProductById(int id)
{
var product = _productRepository.GetProductById(id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
[HttpPost]
public IActionResult AddProduct([FromBody] Product product)
{
if (product == null)
{
return BadRequest();
}
_productRepository.AddProduct(product);
return CreatedAtAction(nameof(GetProductById), new { id = product.Id }, product);
}
[HttpPut("{id}")]
public IActionResult UpdateProduct(int id, [FromBody] Product product)
{
if (product == null || id != product.Id)
{
return BadRequest();
}
var existingProduct = _productRepository.GetProductById(id);
if (existingProduct == null)
{
return NotFound();
}
_productRepository.UpdateProduct(product);
return NoContent();
}
[HttpDelete("{id}")]
public IActionResult DeleteProduct(int id)
{
var existingProduct = _productRepository.GetProductById(id);
if (existingProduct == null)
{
return NotFound();
}
_productRepository.DeleteProduct(id);
return NoContent();
}
}
}
Step 7. Open the app settings file and add the database connection string.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=DESKTOP-8RL8JOG;Initial Catalog=ReactNetCoreCrudDb;User Id=sa;Password=database@1;"
}
}
Step 8. Register our services inside the service container and configure the middleware.



Luis Galindo SanchezPosted Dec 6, 2025, 3:52 AM
Thanks so much...