POST, GET, PUT, DELETE using Web API
Here’s a complete [Link] Core Web API study material focused on implementing POST, GET, PUT,
DELETE operations – the building blocks of CRUD using HTTP methods.
[Link] Core Web API: POST, GET, PUT, DELETE Operations
1. What Are HTTP Verbs?
These map directly to CRUD operations in APIs:
HTTP Verb Action Purpose
GET Read Fetch data
POST Create Add new data
PUT Update Modify entire record
DELETE Delete Remove record
2. Setup: Model, Context, Controller
A. Product Model (Models/[Link])
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
B. DbContext (Data/[Link])
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
C. Configure DB in [Link]:
[Link]<AppDbContext>(options =>
[Link]("ProductDb"));
3. Web API Controller (with all HTTP methods)
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly AppDbContext _context;
public ProductsController(AppDbContext context)
{
_context = context;
}
// GET: api/products
[HttpGet]
public async Task<IActionResult> GetAll()
{
var products = await _context.[Link]();
return Ok(products);
}
1|Page
POST, GET, PUT, DELETE using Web API
// GET: api/products/1
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
var product = await _context.[Link](id);
if (product == null) return NotFound();
return Ok(product);
}
// POST: api/products
[HttpPost]
public async Task<IActionResult> Create(Product product)
{
_context.[Link](product);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(Get), new { id = [Link] }, product);
}
// PUT: api/products/1
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, Product product)
{
if (id != [Link]) return BadRequest();
_context.Entry(product).State = [Link];
await _context.SaveChangesAsync();
return NoContent();
}
// DELETE: api/products/1
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
var product = await _context.[Link](id);
if (product == null) return NotFound();
_context.[Link](product);
await _context.SaveChangesAsync();
return NoContent();
}
}
4. Test With Postman or Swagger
Sample Endpoints:
Method URL Action
GET api/products Get all products
GET api/products/1 Get product by ID
POST api/products Create a new product
2|Page
POST, GET, PUT, DELETE using Web API
Method URL Action
PUT api/products/1 Update product with ID 1
DELETE api/products/1 Delete product with ID 1
Sample POST Body:
{
"id": 1,
"name": "Notebook"
}
Sample PUT Body:
{
"id": 1,
"name": "Updated Notebook"
}
5. Return Types Best Practices
Method Return Type Description
GET Ok(object) 200 OK with JSON
POST CreatedAtAction() 201 Created with route info
PUT NoContent() 204 No Content (on success)
DELETE NoContent() 204 No Content
Error NotFound() / BadRequest() 404 or 400 respectively
Summary
Operation HTTP Verb Method in Controller
Create POST Create(Product product)
Read All GET GetAll()
Read One GET Get(int id)
Update PUT Update(int id, Product)
Delete DELETE Delete(int id)
3|Page