liveBook · Manning

ProblemDetail

As explained in Building Web APIs with ASP.NET Core

A problem detail is a machine-readable representation of an HTTP API error containing fields such as title, detail, and status.

ProblemDetail is a machine-readable JSON representation of an HTTP API error. It commonly includes fields such as title, detail, status, and type, following RFC 7807. section 2.4.3

Use ProblemDetail as the response produced by the API's centralized error route:

  • Create an ErrorController action at /error and return ControllerBase.Problem(). ASP.NET Core fills default values from the thrown exception. section 2.4.3
  • With Minimal APIs, map the same route in one line: app.MapGet("/error", () => Results.Problem()). section 2.4.3
  • Configure exception handling to forward production errors to /error, while the developer exception page can remain enabled during development. section 2.4.3

Typical response

A server error can return JSON such as {"type":"https://tools.ietf.org/html/rfc7231#section-6.6.1","title":"An error occurred while processing your request.","status":500}. section 2.4.3

using Microsoft.AspNetCore.Mvc;
 
namespace MyBGList.Controllers
{
    [ApiController]
    public class ErrorController : ControllerBase
    {
        [Route("/error")]             #1
        [HttpGet]                     #2
        public IActionResult Error()
        {
            return Problem();         #3
        }
    }
}

Browse the controller-based implementation, including the /error route and its Problem() response. section 2.4.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains the response format and demonstrates controller and Minimal API implementations, but defers custom field configuration.
Examples
Uses an ErrorController, a Minimal API mapping, a test exception route, and a status-500 JSON response.
What it assumes
Assumes basic ASP.NET Core application setup and introduces the controller and Minimal API mechanisms used to produce errors.
What you can do after
Apply it via ASP.NET Core by routing unhandled production exceptions to a standardized JSON error response.
How it connects
Connects HTTP error handling with RFC 7807, exception middleware, controllers, Minimal APIs, and Swagger/OpenAPI.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.