This article builds on CRUD concepts (GET, POST, PUT, PATCH) and focuses on real-world API development with ASP.NET Core.
1. DELETE Method in ASP.NET Core Web API
What is DELETE?
The DELETE HTTP method is used to remove data from the server.
Real-life example:
Deleting a product from an online store's inventory.
Example Scenario: Delete a Product
Product Model
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
API Code (DELETE)
[HttpDelete("{id}")]
public IActionResult DeleteProduct(int id)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null)
return NotFound();
products.Remove(product);
return Ok($"Product with ID {id} deleted");
}
Input (Request)
DELETE /api/products/2
Output (Response)
"Product with ID 2 deleted"
After Deletion – GET Output
[
{ "id": 1, "name": "Laptop", "price": 1200 }
]
2. Testing Web API Using Postman (Step-by-Step)
What is Postman?
Postman is a tool used to test APIs by sending HTTP requests and viewing responses.
Step 1: Open Postman
Step 2: Test GET
https://localhost:5001/api/products
Step 3: Test POST
https://localhost:5001/api/products
{
"name": "Keyboard",
"price": 45
}
Step 4: Test PUT
https://localhost:5001/api/products/1
{
"name": "Gaming Laptop",
"price": 1500
}
Step 5: Test PATCH
https://localhost:5001/api/products/1
{
"price": 1400
}
Step 6: Test DELETE
https://localhost:5001/api/products/1
3. Using Real Database with Entity Framework Core
What is Entity Framework Core?
EF Core is an Object Relational Mapper (ORM) that lets you work with databases using C# classes instead of SQL.
Step 1: Install Packages
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
Step 2: Create DbContext
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options) { }
public DbSet<Product> Products { get; set; }
}
Step 3: Configure in Program.cs
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Step 4: Controller Using EF Core
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
private readonly AppDbContext _context;
public ProductsController(AppDbContext context)
{
_context = context;
}
[HttpGet]
public IActionResult GetProducts()
{
return Ok(_context.Products.ToList());
}
[HttpPost]
public IActionResult AddProduct(Product product)
{
_context.Products.Add(product);
_context.SaveChanges();
return Ok(product);
}
[HttpDelete("{id}")]
public IActionResult DeleteProduct(int id)
{
var product = _context.Products.Find(id);
if (product == null)
return NotFound();
_context.Products.Remove(product);
_context.SaveChanges();
return Ok("Deleted successfully");
}
}
4. Interview-Ready Explanation (With Example)
Q1: What is the difference between GET and POST?
Answer:
Example:
Q2: Difference between PUT and PATCH?
Answer:
Q3: When do you use DELETE?
Answer:
DELETE is used to remove a resource permanently from the system.
Example:
DELETE /api/products/5
Q4: Why use Entity Framework Core?
Answer:
No need to write SQL
Faster development
Strongly typed data
Database-agnostic
Q5: How do you test APIs?
Answer:
Using tools like Postman or Swagger to send HTTP requests and verify responses.
Summary
| Feature | Purpose |
|---|
| DELETE | Remove data |
| Postman | Test APIs |
| EF Core | Database operations |
| Interview Prep | Concept clarity |