HttpStatusExceptions.AspNetCore
1.0.3
dotnet add package HttpStatusExceptions.AspNetCore --version 1.0.3
NuGet\Install-Package HttpStatusExceptions.AspNetCore -Version 1.0.3
<PackageReference Include="HttpStatusExceptions.AspNetCore" Version="1.0.3" />
<PackageVersion Include="HttpStatusExceptions.AspNetCore" Version="1.0.3" />
<PackageReference Include="HttpStatusExceptions.AspNetCore" />
paket add HttpStatusExceptions.AspNetCore --version 1.0.3
#r "nuget: HttpStatusExceptions.AspNetCore, 1.0.3"
#:package HttpStatusExceptions.AspNetCore@1.0.3
#addin nuget:?package=HttpStatusExceptions.AspNetCore&version=1.0.3
#tool nuget:?package=HttpStatusExceptions.AspNetCore&version=1.0.3
HttpStatusExceptions
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 | Versions 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. |
-
net10.0
- HttpStatusExceptions (>= 1.0.3)
-
net8.0
- HttpStatusExceptions (>= 1.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.