An error response tells an API consumer that a requested operation failed or could not be completed. In a minimal API application, it can result from an endpoint returning an error status code, missing route handling, authentication middleware, or an exception. Consistent APIs represent these failures with Problem Details JSON. section 5.3.3
Handle the two main error classes differently:
- Exceptions are caught by ExceptionHandlerMiddleware or DeveloperExceptionPageMiddleware and can be converted automatically to Problem Details after registering IProblemDetailsService with AddProblemDetails(). section 5.3.3
- Error status code responses without a body can be completed by StatusCodePagesMiddleware, which adds a Problem Details response body. section 5.3.3
Returning a Problem Details body for a 404
Register Problem Details, add StatusCodePagesMiddleware, and return Results.NotFound() from an endpoint. The middleware supplies the response body automatically, giving consumers a consistent JSON error shape. section 5.3.3
