DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult 2025.0.10

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult --version 2025.0.10
                    
NuGet\Install-Package DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult -Version 2025.0.10
                    
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="DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult" Version="2025.0.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult" Version="2025.0.10" />
                    
Directory.Packages.props
<PackageReference Include="DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult --version 2025.0.10
                    
#r "nuget: DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult, 2025.0.10"
                    
#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.
#:package DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult@2025.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult&version=2025.0.10
                    
Install as a Cake Addin
#tool nuget:?package=DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult&version=2025.0.10
                    
Install as a Cake Tool

ASP.NET Core WebApp Self-Documented Problem Result Extension

© 2025 DotNet Brightener

NuGet Version

Introduction

Have you ever wanted to have a consistent way of returning errors from your ASP.NET Core Web application? This package provides an abstraction for responding errors from your application to the client, base on the RFC 9457 specification.

When the application encounter an error, it should return a ProblemDetails object that contains information about the error.

This package

  • Added a global exception handler to catch unhandled exceptions and return a ProblemDetails object. An ILogger is also added to log the exception automatically for the unhandled exceptions.

  • Provides a based IProblembResult interface and its extension methods ToProblemDetails() or ToProblemResult(), to create consistent error responses. The error response format is based on the RFC 9457 specification.

When your application needs to response the error, you can either throw an exception derived from BaseProblemDetailsError class or simply create a class that implements IProblemResult interface. The error object will be converted to ProblemDetails object and returned to the client. Check Usage section for more information.

Installation

You can install the package from NuGet:


dotnet add package DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult

Usage

1. Enable the global exception handler

Add the following code to your Startup.cs (if you use Startup.cs) or Program.cs (by default) file:


// this can be omitted if your application already added IHttpContextAccessor
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

// The default way of handling unhandled exceptions
builder.Services.AddExceptionHandler<UnhandledExceptionResponseHandler>();

// Adds services required for creation of <see cref="ProblemDetails"/> for failed requests.
builder.Services.AddProblemDetails();


// Add the following to your Configure method, 
// or after 
// var app = builder.Build(); if you use Program.cs
app.UseExceptionHandler();

After the configuration above, if your application throws an exception, the response will be a ProblemDetails object.

2. Create a consistent error response

2.1. Using Exception approach

Traditionally we used to throw exceptions when there are errors. You can create an exception class that inherits BaseProblemDetailsError class. The <summary> and <remarks> XML comments will be used to generate the ProblemDetails object.


/// <summary>
///     The error represents the requested object of type `User` could not be found
/// </summary>
/// <remarks>
///     The error is thrown because the requested resource of type `User` could not be found
/// </remarks>
public class UserNotFoundException : BaseProblemDetailsError
{
    public UserNotFoundException()
        : base("User Not Found", HttpStatusCode.BadRequest)
    {
    }

    public UserNotFoundException(long userId)
        : this()
    {
        Data.Add("userId", userId);
    }
}

Somewhere in your application, where an error is expected, you can throw the exception as followed:

// UserService.cs

    public User GetUser(long userId)
    {
        var user = _userRepository.GetUser(userId);

        if (user == null)
        {
            throw new UserNotFoundException(userId);
        }

        return user;
    }

Without having to handle the exception, the error will be caught by the global exception handler and return a ProblemDetails object.

// UserController.cs

    [HttpGet("{userId}")]
    public IActionResult GetUserDetail(long userId)
    {
        var user = _userService.GetUser(userId);

        // Without handling the exception, the error will be caught by the global exception handler

        return Ok(user);
    }

2.2. Using ProblemResult approach

Create a class that inherits BaseProblemDetailsError. The <summary> and <remarks> XML comments will be used to generate the ProblemDetails object.


using AspNet.Extensions.SelfDocumentedProblemResult.ErrorResults;

/// <summary>
///     The error represents the requested object of type `User` could not be found
/// </summary>
/// <remarks>
///     The error is thrown because the requested resource of type `User` could not be found
/// </remarks>
public class UserNotFoundError : BaseProblemDetailsError
{
    public UserNotFoundError()
        : base(HttpStatusCode.NotFound)
    {

    }

    public UserNotFoundError(long userId)
        : this()
    {
        Data.Add("userId", userId);
    }
}

// UserService.cs

    public User GetUser(long userId)
    {
        var user = _userRepository.GetUser(userId);

        return user;
    }

Somewhere in your controller, where an error is expected, you can return the error like this:

// UserController.cs
    [HttpGet("{userId}")]
    public IActionResult GetUserDetail(long userId)
    {
        var user = _userService.GetUser(userId);
        if (user == null)
        {
            // Explicitly return the error
            var error = new UserNotFoundError(userId);

            return error.ToProblemResult();
        }

        // Omited for brevity
    }

In the above snippet, where the user is not found, a response of status code 404 will be returned with the following body:


{
   "type": "UserNotFoundError",
   "title": "User Not Found Error",
   "status": 404,
   "detail": "The error is thrown because the requested resource of type `User` could not be found",
   "instance": "/users/123",
   "data": {
       "userId": 123
   }
}

The XML comments for the class will be used to generate the detail information about the error. It can be useful if you use the UI package, as the error information can be obtain via the UI.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult:

Package Downloads
DotNetBrightener.WebApi.GenericCRUD

Package Description

DotNetBrightener.WebApp.CommonShared

Package Description

DotNetBrightener.LocaleManagement

Package Description

DotNetBrightener.AspNet.Extensions.SelfDocumentedProblemResult.UI

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.0.1-preview-728 97 1/20/2026
2026.0.1-preview-723 117 1/15/2026
2026.0.1-preview-721 116 1/15/2026
2026.0.1-preview-717 141 1/5/2026
2026.0.1-preview-713 498 12/10/2025
2026.0.1-preview-700 250 12/7/2025
2026.0.1-preview-689 141 12/6/2025
2026.0.1-preview-676 707 12/2/2025
2026.0.1-preview-671 152 11/29/2025
2026.0.1-preview-670 154 11/29/2025
2026.0.1-preview-669 142 11/29/2025
2026.0.1-preview-667 213 11/27/2025
2026.0.1-preview-665 209 11/27/2025
2026.0.1-preview-664 211 11/27/2025
2026.0.1-preview-660 220 11/25/2025
2025.0.10 242 11/25/2025
2025.0.10-preview-628 462 11/19/2025
2025.0.10-preview-605 323 11/17/2025
2025.0.10-preview-602 333 11/17/2025
2025.0.10-preview-581 359 11/11/2025
2025.0.10-preview-559 254 10/29/2025
2025.0.10-preview-557 252 10/27/2025
2025.0.9 246 10/26/2025
2025.0.9-preview-553 236 10/26/2025
2025.0.9-preview-539 436 10/12/2025
2025.0.9-preview-535 226 10/8/2025
2025.0.9-preview-509 261 10/2/2025
2025.0.9-preview-508 238 10/2/2025
2025.0.9-preview-487 266 9/29/2025
2025.0.9-preview-481 250 9/29/2025
2025.0.9-preview-473 223 9/28/2025
2025.0.8 254 9/23/2025
2025.0.6 250 9/22/2025
2025.0.6-preview-455 261 9/22/2025
2025.0.6-preview-454 350 9/17/2025
2025.0.6-preview-441 192 9/14/2025
2025.0.6-preview-440 195 9/14/2025
2025.0.6-preview-406 225 9/2/2025
2025.0.6-preview-401 208 9/2/2025
2025.0.6-preview-400 225 9/2/2025
2025.0.6-preview-369 249 8/27/2025
2025.0.6-preview-368 251 8/27/2025
2025.0.6-preview-334 245 8/18/2025
2025.0.6-preview-333 162 7/27/2025
2025.0.6-preview-332 530 7/24/2025
2025.0.6-preview-331 535 7/24/2025
2025.0.6-preview-328 540 7/24/2025
2025.0.6-preview-327 412 7/21/2025
2025.0.6-preview-326 418 7/21/2025
2025.0.6-preview-325 321 7/20/2025
2025.0.6-preview-324 323 7/20/2025
2025.0.6-preview-322 322 7/20/2025
2025.0.6-preview-321 329 7/19/2025
2025.0.6-preview-320 327 7/19/2025
2025.0.6-preview-319 238 7/17/2025
2025.0.6-preview-317 225 7/17/2025
2025.0.6-preview-316 234 7/17/2025
2025.0.6-preview-315 223 7/17/2025
2025.0.6-preview-314 237 7/17/2025
2025.0.6-preview-313 233 7/17/2025
2025.0.6-preview-312 226 7/16/2025
2025.0.5 247 7/10/2025
2025.0.5-preview-307 165 7/5/2025
2025.0.4 162 7/5/2025
2025.0.4-preview-305 201 7/4/2025
2025.0.4-preview-304 234 7/1/2025
2025.0.4-preview-299 220 5/31/2025
2025.0.4-preview-298 186 5/30/2025
2025.0.4-preview-296 215 5/30/2025
2025.0.4-preview-295 233 5/29/2025
2025.0.4-preview-293 234 5/26/2025
2025.0.4-preview-292 242 5/26/2025
2025.0.3 218 2/10/2025
2025.0.3-preview-288 204 2/10/2025
2025.0.2 196 1/21/2025
2025.0.2-preview-278 197 1/21/2025
2025.0.2-preview-277 205 12/16/2024
2025.0.1-rc-243301701 490 11/25/2024
2024.0.14.6 230 11/25/2024
2024.0.14.6-rc-243031001 289 10/29/2024
2024.0.14.6-rc-243030701 227 10/29/2024
2024.0.14.6-rc-242840501 218 10/10/2024
2024.0.14.6-rc-242820305 208 10/8/2024
2024.0.14.6-rc-242771401 355 10/3/2024
2024.0.14.6-rc-242770501 203 10/3/2024
2024.0.14.6-rc-242770201 230 10/3/2024
2024.0.14.6-rc-242761801 206 10/2/2024
2024.0.14.6-rc-242761601 231 10/2/2024
2024.0.14.6-rc-242761501 201 10/2/2024
2024.0.14.6-rc-242761401 232 10/2/2024
2024.0.14.6-rc-242760701 228 10/2/2024
2024.0.14.6-rc-242751002 214 10/1/2024
2024.0.14.6-rc-242750901 252 10/1/2024
2024.0.14.6-rc-242750502 212 10/1/2024
2024.0.14.6-rc-242750201 219 10/1/2024
2024.0.14.6-rc-242741501 212 9/30/2024
2024.0.14.6-rc-242730701 224 9/29/2024
2024.0.14.6-preview-2730501 191 9/29/2024
2024.0.14.6-preview-2701501 264 9/26/2024
2024.0.14.6-preview-2620901 269 9/18/2024
2024.0.14.6-preview-2570701 217 9/13/2024
2024.0.14.6-preview-2510703 289 9/7/2024
2024.0.14.6-preview-2480501 246 9/4/2024
2024.0.14.6-preview-2430401 260 8/30/2024
2024.0.14.6-preview-242730701 220 9/29/2024
2024.0.14.6-preview-2421703 222 8/29/2024
2024.0.14.6-preview-2421701 213 8/29/2024
2024.0.14.6-preview-2420901 224 8/29/2024
2024.0.14.6-preview-2390101 257 8/26/2024
2024.0.14.6-preview-2381603 242 8/25/2024
2024.0.14.6-preview-2341601 250 8/21/2024
2024.0.14.6-preview-2321602 233 8/20/2024
2024.0.14.6-preview-2190801 262 8/6/2024
2024.0.14.6-preview-2041501 192 7/22/2024
2024.0.14.6-preview-1920603 298 7/10/2024
2024.0.14.6-preview-1920301 220 7/10/2024
2024.0.14.6-preview-1911302 232 7/9/2024
2024.0.14.6-preview-1901001 223 7/8/2024
2024.0.14.6-preview-1900901 218 7/8/2024
2024.0.14.6-preview-1900801 205 7/8/2024
2024.0.14.6-preview-1860304 195 7/4/2024
2024.0.14.5 263 7/1/2024
2024.0.14.5-preview-1811601 216 6/29/2024
2024.0.14.5-preview-1810501 232 6/29/2024
2024.0.14.5-preview-180132 222 6/28/2024
2024.0.14.5-preview-180131 195 6/28/2024
2024.0.14.5-preview-180121 196 6/28/2024
2024.0.14.4 247 6/27/2024
2024.0.14.4-preview-8 166 6/27/2024
2024.0.14.4-preview-7 219 6/27/2024
2024.0.14.4-preview-6 154 6/27/2024
2024.0.14.3 216 6/21/2024
2024.0.14.1 235 6/6/2024
2024.0.14.1-preview 199 6/6/2024
2024.0.14-preview-1 192 6/6/2024
2024.0.13.8-preview 203 6/6/2024
2024.0.13.2-preview-247 161 6/6/2024
2024.0.13.1-preview-0146 199 6/6/2024
2024.0.13-preview-1 172 6/6/2024
2024.0.12.15803-preview-03 196 6/6/2024
2024.0.12.15608 226 6/4/2024
2024.0.12.15515 286 6/3/2024
2024.0.12.15220 222 5/31/2024
2024.0.12.15220-alpha31-240... 193 5/31/2024
2024.0.12.14911 259 5/28/2024
2024.0.12.14910-alpha28-240... 197 5/28/2024
2024.0.12.14823 248 5/27/2024
2024.0.12.14522-alpha7-2405... 211 5/24/2024
2024.0.12.14514-alpha6-2405... 203 5/24/2024
2024.0.12.14511 247 5/24/2024
2024.0.12.14314 278 5/22/2024
2024.0.12.14114 259 5/20/2024
2024.0.12.12815 277 5/7/2024
2024.0.12.12814 220 5/7/2024
2024.0.12.12721 240 5/6/2024
2024.0.12.12702 236 5/5/2024
2024.0.12.12622 253 5/5/2024
2024.0.12.12514 240 5/4/2024
2024.0.12.12512 268 5/4/2024
2024.0.12.12510 259 5/4/2024
2024.0.12.12420 191 5/3/2024
2024.0.12.12319 195 5/2/2024
2024.0.12.12319-rc-2405021801 173 5/2/2024
2024.0.12.12318 191 5/2/2024
2024.0.12.12215 243 5/1/2024
2024.0.12.12011 249 4/29/2024
2024.0.12.11720 243 4/26/2024
2024.0.12.11719 245 4/26/2024
2024.0.12.11621 259 4/25/2024
2024.0.12.11523 234 4/24/2024
2024.0.12.11522 256 4/24/2024
2024.0.12.11417 235 4/23/2024
2024.0.12.11400 246 4/22/2024
2024.0.12.11316 265 4/22/2024
2024.0.11.10220 252 4/11/2024
2024.0.11.10120 223 4/10/2024
2024.0.11.10119 201 4/10/2024
2024.0.11.10115 215 4/10/2024
2024.0.11.9914 259 4/8/2024
2024.0.11.9901 227 4/7/2024
2024.0.11.9823 243 4/7/2024
2024.0.11.9401 254 4/2/2024
2024.0.11.9301 230 4/1/2024
2024.0.11.9206 260 3/31/2024
2024.0.11.9205 261 3/31/2024