ProblemDetails 1.0.0

Suggested Alternatives

Problem.Details

Additional Details

Changed the namespace

dotnet add package ProblemDetails --version 1.0.0
NuGet\Install-Package ProblemDetails -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ProblemDetails" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProblemDetails --version 1.0.0
#r "nuget: ProblemDetails, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ProblemDetails as a Cake Addin
#addin nuget:?package=ProblemDetails&version=1.0.0

// Install ProblemDetails as a Cake Tool
#tool nuget:?package=ProblemDetails&version=1.0.0

ProblemDetails

Quality Gate Status Coverage

Problem

By default, the ASP.NET MVC Core framework doesn't return consistent error responses across different statuses. For example, the framework will return a status 500 with an empty body when encountering an internal server error. Model validation errors will send back a JSON body, but the field names don't follow the casing rules. This package is meant to unify the responses.

Solution

All response codes above 400 will have a nice JSON response, for example:

Status 404

{
  "type": "https://httpstatuses.com/404",
  "title": "Error 404",
  "status": 404
}

Status 400 (note that the field names are camel-cased)

{
  "errors": {
    "requiredField": ["The requiredField field is required."]
  },
  "type": "https://httpstatuses.com/400",
  "title": "One or more validation errors occurred.",
  "status": 400
}

Status 500

{
  "type": "https://httpstatuses.com/500",
  "title": "Error 500",
  "status": 500
}

You can also override the title values

Getting started

  1. Install the package
Install-Package ProblemDetails

Or via the .NET Core command line interface:

dotnet add package ProblemDetails
  1. Call services.AddProblemDetails() in services configure method:
public void ConfigureServices(IServiceCollection services)
{
    services.AddProblemDetails()
      .MapStatusToTitle(500, "500, Oops!"); // optional overrides
  1. Call app.UseProblemDetails() in app Configure method:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseProblemDetails();

Check the sample project or browse source

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 462 6/26/2021

Initial release