ASP.NET Core Web API Interview Question #44

What does CreatedAtAction do?

DTOs, Model Binding, Validation & API Responses Junior Beginner

Quick Interview Answer

CreatedAtAction returns HTTP 201 Created and generates a Location header pointing to another action, typically the GET endpoint for the newly created resource. It can also include the created representation in the response body.

Detailed Explanation

CreatedAtAction returns HTTP 201 Created and generates a Location header pointing to another action, typically the GET endpoint for the newly created resource. It can also include the created representation in the response body.

This is a strong pattern for resource creation because the client immediately knows how to retrieve the newly created resource.

Code Example

var product = await _service.CreateAsync(request, token);

return CreatedAtAction(
    nameof(GetById),
    new { id = product.Id },
    product);