Jason Rowe

Be curious! Choose your own adventure.

ASP.NET Core Problem Details examples

In versions 2.1 and 2.2, ASP.NET Core added a feature called “Problem Details” which will help you standardize error messages in API controllers. In 2.2, file new project enables “Problem Details” by default for errors thrown from controllers using the “[ApiController]” attribute. Also with the help from a nuget package Hellang.Middleware.ProblemDetails you can add these same errors for 404 and 500 errors outside of API controllers. So with an upgrade to 2.2 and a nuget package your API can get all your errors standardized to a defined specification (RFC 7807). :)

Here are the official release notes about the topic I could find.

ASP.NET Core 2.1 Problem Details support

ASP.NET Core 2.2 release notes on Problem Details

Upgrade from 2.0 to 2.2 and use Problem Details

If you are upgrading an existing site to 2.2 and want to use Problem Details you will need to add the following code. You can also do a file new project and copy these values from the startup. In 3.0 they will probably remove these compatibility switches.

		public void ConfigureServices(IServiceCollection services)
		{
			services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
		}

Then make sure your controller is using the “ApiController” attribute. One other cool thing they added in 2.2 is that you can move the ApiController up to the an assembly, so you don’t have to add it to every controller.

In ASP.NET Core 2.2 or later, the [ApiController] attribute can be applied to an assembly. Annotation in this manner applies web API behavior to all controllers in the assembly. Beware that there’s no way to opt out for individual controllers.

[Route("api/[controller]")]
	[ApiController]
	public class ValuesController : ControllerBase

Now if you make a request that causes a validation error you should see something like the following:

Error generated by making a request to the values controller included in a file new project API template /api/values/foo).

{
"errors": {
"id": [
"The value 'foo' is not valid."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "0HLJIMARUILN4:00000001"
}

Use Problem Details for 400 and 500 errors

Now if you try making a 404 request that doesn’t hit a controller you won’t get the problem details. More information why ASP.NET doesn’t include that by default. ProblemDetails is not returned for 404NotFound and 500Exception. To fix this you can install this nuget plugin Hellang.Middleware.ProblemDetails. Here is an example of how to configure it in Startup.cs after install.

		public void ConfigureServices(IServiceCollection services)
		{
			// Hellang.Middleware.ProblemDetails package
			services.AddProblemDetails();
			services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
		}
		public void Configure(IApplicationBuilder app, IHostingEnvironment env)
		{
			app.UseProblemDetails();

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *