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
