Problem Details responses everywhere with ASP.NET Core and .NET 7

The Problem Details for HTTP APIs RFC provides a unified, machine-readable and standardized recipe for exposing error information out of your HTTP APIs – which is of course beneficial both for the API authors, as well as the integrating parties.

ASP.NET Core has supported problem details since version 2.1, however it was not been uniformly used across all ASP.NET Core components. It was possible to return the Problem Details response manually, or the framework could generate it automatically in several specific cases. Even the official documentation referred to a third-party middleware in order to get a better Problem Details experience.

This is finally changing in .NET 7.

Out of the box support since .NET 7 Preview 7

Thanks to the great work of Bruno Oliveira, ASP.NET Core on .NET 7 is now getting a much better support for Problem Details. Going forward, assuming everything is configured properly, most of the non-success responses and exceptions will be possible to be automatically converted to and exposed to the callers in the Problem Details format.

The feature first appeared in .NET 7 Preview 7, which came out on 9 August 2022. Since this is an official public preview, it can be easily installed and explored.

Configuration

The feature is opt-in – to not creating a breaking change – and the primary configuration mechanism is to add the following call when bootstrapping the services collection of ASP.NET Core:

Once that is in place, there are three primary ways to get Problem Details to become part of your application, each one related to a slightly different aspect of the framework.

First, by adding the default error handler, all unhandled exceptions will be given the Problem Details treatment:

With this in place, any unhandled exception from e.g. an MVC controller, a filter, a minimal API endpoint or a middleware, would get converted to the following response:

Secondly, enabling the status code pages middleware, will allow Problem Details to be used in some extra non-exception related framework scenarios, such as a 404 occurring due to a non-existent route or a 405 occurring due to a caller using an invalid HTTP method on an existing endpoint. This is enabled via:

And then a sample response would be (in this particular case, when calling a POST on a GET endpoint):

Finally, Problem Details can also be used in conjunction with the developer exception page middleware. In that case, a much richer set of information, including the stack trace and the request details, will also be added to the Problem Details response. Because of that, this should obviously be avoided in production environment.

The exception response would then look similar to:

Further customizations

The default behaviors can be further customized in two ways. First of all, the registration method AddProblemDetails() provides an overload that allows configuring some basic options against ProblemDetailsOptions. The typical use case would be to register own delegate which would be invoked when an instance of ProblemDetails model gets created.

The example below shows injecting a custom identifier to the Instance field. Assuming that you would log the identifier into your logging system, it could then be used to easily located the error in the logs – when the caller shares that identifier.

More sophisticated customizations are also possible. The call to AddProblemDetails() actually internally registers the defaults implementations of a new interface IProblemDetailsService, which is responsible for the generation of the Problem Details model, as well as an implementation of IProblemDetailsWriter, which is then responsible for writing the model to the HTTP response stream.

Both of them can be replaced with own custom variants, registered in the DI container – in which case the call to AddProblemDetails() would not longer be needed. At that point we have full control of the creation and writing of the Problem Details response.