HttpStatusExceptions.AspNetCore 1.0.3

dotnet add package HttpStatusExceptions.AspNetCore --version 1.0.3
                    
NuGet\Install-Package HttpStatusExceptions.AspNetCore -Version 1.0.3
                    
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="HttpStatusExceptions.AspNetCore" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HttpStatusExceptions.AspNetCore" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="HttpStatusExceptions.AspNetCore" />
                    
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 HttpStatusExceptions.AspNetCore --version 1.0.3
                    
#r "nuget: HttpStatusExceptions.AspNetCore, 1.0.3"
                    
#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 HttpStatusExceptions.AspNetCore@1.0.3
                    
#: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=HttpStatusExceptions.AspNetCore&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=HttpStatusExceptions.AspNetCore&version=1.0.3
                    
Install as a Cake Tool

HttpStatusExceptions

HttpStatusExceptions HttpStatusExceptions.AspNetCore License

A lightweight library for throwing HTTP status code exceptions using clean, expressive static helper methods.

Installation

# Base library
dotnet add package HttpStatusExceptions

# ASP.NET Core integration (includes ProblemDetails support)
dotnet add package HttpStatusExceptions.AspNetCore

Usage

Throwing Exceptions

using HttpStatusExceptions;

// Using static throw helpers
HttpStatusException.Throw404NotFound();
HttpStatusException.Throw401Unauthorized("Invalid API key.");

// Or throw directly
throw new HttpStatusException(404, "Resource not found.");

Generic Variants

Generic overloads let you throw in expression contexts—useful for null-coalescing and conditional expressions:

var item = await db.Items.FirstOrDefaultAsync(item => item.Id == id)
    ?? HttpStatusException.Throw404NotFound<Item>("Item not found.");

var result = isValid
    ? ProcessData()
    : HttpStatusException.Throw400BadRequest<Result>();

ProblemDetails Support (ASP.NET Core)

The HttpStatusExceptions.AspNetCore package adds a ToProblemDetails() extension method:

using HttpStatusExceptions.AspNetCore;

// In Program.cs
builder.Services.AddProblemDetails(options =>
{
    options.CustomizeProblemDetails = (ctx) =>
    {
        ctx.ProblemDetails = MapProblemDetails(ctx.Exception);
    };
});

static ProblemDetails MapProblemDetails(Exception? ex)
    => ex switch
    {
        HttpStatusException httpEx => httpEx.ToProblemDetails(),
        _ => new ProblemDetails
        {
            Detail = "An unexpected error occurred.",
            Status = StatusCodes.Status500InternalServerError,
        }
    };

Supported Status Codes

All standard 4xx and 5xx status codes are supported:

Category Status Codes
4xx Client Errors 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 421, 422, 423, 424, 426, 428, 429, 431, 451
5xx Server Errors 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511

Additionally, 405 Method Not Allowed has method-specific helpers: Throw405GetNotAllowed(), Throw405PostNotAllowed(), Throw405PutNotAllowed(), Throw405DeleteNotAllowed(), etc.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  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 is compatible.  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.

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.3 32 3/6/2026
1.0.2 98 1/15/2026
1.0.1 94 1/15/2026
1.0.0 98 1/15/2026