ASP.NET Core Web API Interview Question #49

How should null and empty results be handled in API responses?

DTOs, Model Binding, Validation & API Responses Junior Beginner

Quick Interview Answer

A request for one specific resource normally returns 404 when that resource does not exist. A valid collection query that finds no matching records normally returns 200 with an empty collection rather than 404.

Detailed Explanation

A request for one specific resource normally returns 404 when that resource does not exist. A valid collection query that finds no matching records normally returns 200 with an empty collection rather than 404.

This distinction gives clients predictable semantics: the collection resource exists even when it currently contains zero matching items.

Code Example

// Single resource:
if (product == null)
    return NotFound();

// Collection:
return Ok(Array.Empty<ProductResponse>());