Tutorials ASP.NET Core Web API Tutorial
HTTP (HyperText Transport Protocol) — Complete Guide
HTTP (HyperText Transport Protocol) — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core Web API Tutorial on Toolliyo Academy.
On this page
ASP.NET Core Web API Tutorial · Lesson 11 of 100
Create API in ASP.NET Core Web API
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — API foundations · ~6 min · Module 2: CRUD APIs
What is this?
Create means HTTP POST — the client sends JSON in the body, your API validates it, saves a new row, and returns 201 Created with the new resource (often including the new id).
Why should you care?
Every catalog, signup form, and order placement starts with POST. Wrong status codes or missing Location headers confuse mobile apps.
See it live — copy this example
Create a Web API (dotnet new webapi), paste the example, run dotnet run, test in Swagger.
[HttpPost]
public async Task<ActionResult<ProductDto>> Create(
[FromBody] CreateProductDto dto, CancellationToken ct)
{
if (!ModelState.IsValid) return ValidationProblem(ModelState);
var created = await _service.CreateAsync(dto, ct);
return CreatedAtAction(nameof(Get), new { id = created.Id }, created);
}
Run Example »
Edit the code and click Run — like W3Schools Try it Yourself.
What happened?
- CreatedAtAction builds 201 response and Location header pointing to GET /api/products/{id}.
- Validate before SaveChanges so bad data never hits SQL.
Try it yourself
- Create CreateProductDto with Name and Price.
- Add POST action on ProductsController.
- Test valid JSON in Swagger — expect 201 and body with id.
- Change a route URL or DTO property and save — test again in Swagger or curl.
- Return the wrong status code on purpose (404 instead of 200) and see what the client shows.
Remember
POST creates resources. Return 201 with created DTO. Validate input before database write.