liveBook · Manning

Problem Details

As explained in ASP.NET Core in Action, 3e

Problem Details is a standardized structured format for representing HTTP error information in a response body, commonly serialized as JSON.

Problem Details standardizes machine-readable error responses for HTTP APIs. It gives clients a consistent JSON structure instead of requiring a different error format for every endpoint. section 5.3.2

Minimal APIs provide Results.Problem() for general errors and Results.ValidationProblem() for validation errors. Problem() defaults to status 500, while ValidationProblem() defaults to 400 and accepts a dictionary of validation messages. section 5.3.2

Consistent Problem Details responses can cover errors generated in several places:

  • Endpoint handlers can return Problem Details directly for conditions such as a missing resource or duplicate identifier. section 5.3.2
  • ExceptionHandlerMiddleware can convert unhandled exceptions automatically when AddProblemDetails() registers the IProblemDetailsService. section 5.3.3
  • StatusCodePagesMiddleware can add a Problem Details body to error responses that otherwise have no response body. section 5.3.3

Duplicate fruit identifier

A POST request that reuses a fruit identifier returns ValidationProblem() with an id error such as “A fruit with this id already exists.” The client receives validation information in the standard JSON format. section 5.3.2

Listing 5.5 Returning Problem Details using Results.Problem
using System.Collections.Concurrent;
 
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
 
var _fruit = new ConcurrentDictionary<string, Fruit>();
 
app.MapGet("/fruit", () => _fruit);
 
app.MapGet("/fruit/{id}", (string id) =>
    _fruit.TryGetValue(id, out var fruit)
        ? TypedResults.Ok(fruit)
        : Results.Problem(statusCode: 404));                           #1
 
 
app.MapPost("/fruit/{id}", (string id, Fruit fruit) =>
    _fruit.TryAdd(id, fruit)
        ? TypedResults.Created($"/fruit/{id}", fruit)
        : Results.ValidationProblem(new Dictionary<string, string[]>   #2
          {                                                            #2
              {"id", new[] {"A fruit with this id already exists"}}    #2
          }));                                                         #2
#1 Returns a Problem Details object with a 404 status code
#2 Returns a Problem Details object with a 400 status code and includes the validation errors

Inspect the minimal API example that returns a 404 Problem Details response and a validation response for duplicate fruit identifiers. section 5.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines fields and demonstrates endpoint helpers, middleware integration, exception conversion, and empty error-body handling.
Examples
Uses a fruit API with missing-resource responses, duplicate identifiers, validation dictionaries, exceptions, and empty 404 responses.
What it assumes
Assumes basic minimal API endpoints and HTTP status codes while teaching standardized error formatting and automatic middleware handling.
What you can do after
Build it from scratch using Results helpers and configure middleware to produce consistent Problem Details JSON across an API.
How it connects
Connects HTTP APIs with minimal APIs, JSON, validation errors, exception handling middleware, and status code pages.

Also explained in

Compare how 2 books explain Problem Details →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.