Elyspio.Utils.Results.AspNetCore
1.0.0
dotnet add package Elyspio.Utils.Results.AspNetCore --version 1.0.0
NuGet\Install-Package Elyspio.Utils.Results.AspNetCore -Version 1.0.0
<PackageReference Include="Elyspio.Utils.Results.AspNetCore" Version="1.0.0" />
<PackageVersion Include="Elyspio.Utils.Results.AspNetCore" Version="1.0.0" />
<PackageReference Include="Elyspio.Utils.Results.AspNetCore" />
paket add Elyspio.Utils.Results.AspNetCore --version 1.0.0
#r "nuget: Elyspio.Utils.Results.AspNetCore, 1.0.0"
#:package Elyspio.Utils.Results.AspNetCore@1.0.0
#addin nuget:?package=Elyspio.Utils.Results.AspNetCore&version=1.0.0
#tool nuget:?package=Elyspio.Utils.Results.AspNetCore&version=1.0.0
Elyspio.Utils.Results
A Result type for expected failures, built on C# 15 unions (.NET 11). Exceptions stay for the unexpected.
dotnet add package Elyspio.Utils.Results
dotnet add package Elyspio.Utils.Results.AspNetCore # ToHttpResult / ProblemDetails
Result
public Result<Stock> GetStock(string grid)
{
if (!IsValid(grid)) return new InvalidGrid(grid); // error => failure
return new Stock(grid, amounts); // value => success
}
Result<T, TError>holds eitherOk<T>orErr<TError>. Values are stored typed, with no boxing. Aswitchover the two cases is exhaustive:
var text = result switch
{
Ok<Stock> ok => $"{ok.Value.Amounts.Count} entries",
Err<Error> { Error: Unavailable } => "mod down",
Err<Error> err => err.Error.Code,
};
Result<T>isResult<T, Error>, the common case.TryGet(out value, out error)tests and unpacks in one step:if (!read.TryGet(out var amounts, out var error)) return trace.Fail(error);MapandBindare synchronous.Unit.Valueis the success of an operation that returns nothing (Result<Unit>).default(Result<T>)is neither a success nor a failure:TryGet,MapandBindthrow on it.Failed/Failed<TError>, returned bytrace.Fail(...)in Elyspio.Utils.Telemetry, convert to anyResultwithout namingT.trace.Fail(error)gives aFailed<Error>(Result<T>andResult<T, Error>); for a method returningResult<T, MyError>withMyError : Error, name the type:trace.Fail<MyError>(error).- Name clash:
Ok<T>also exists inMicrosoft.AspNetCore.Http.HttpResults. Do not import both namespaces in the same file.
Errors
Error(Code, Message) is a closed record. Only the categories below derive from it directly, so a switch over the categories needs no default arm. The categories themselves are open, so an app derives its own errors from them:
| Category | HTTP |
|---|---|
Invalid (+ Fields for per-field messages) |
400 |
Unauthorized |
401 |
Forbidden |
403 |
NotFound |
404 |
Conflict |
409 |
TooManyRequests |
429 |
Unexpected (message never shown to the caller) |
500 |
BadGateway |
502 |
Unavailable |
503 |
Timeout |
504 |
Failure(Status, ...) |
any |
public sealed record InvalidResourceId(string ResourceId) : Invalid("ores.invalid-resource-id", "Identifiant de ressource invalide.")
{
public override IEnumerable<KeyValuePair<string, object?>> Extensions => [new("resourceId", ResourceId)];
}
Code is stable: it becomes the ProblemDetails type (urn:problem:{Code}) and the span's error.type. Extensions are published to the caller only when the error overrides them (opt-in).
Add <WarningsAsErrors>$(WarningsAsErrors);CS8509</WarningsAsErrors> to the app so that a non-exhaustive switch breaks the build.
ASP.NET Core
builder.Services.AddProblemDetails();
app.UseExceptionHandler(); // unexpected exceptions: 500 ProblemDetails
app.MapGet("/api/ores/stock/{grid}", async (string grid, OresService ores, CancellationToken ct) => (await ores.GetStock(grid, ct)).ToHttpResult());
app.MapPost("/api/ores/refresh/{grid}", (string grid, OresService ores) => ores.Refresh(grid).ToHttpResult(run => TypedResults.Accepted($"/api/ores/refresh/{run.Id}", run)));
ToHttpResult()maps a success to 200 with the value (204 forResult<Unit>). The overloadToHttpResult(onSuccess)builds any other success response (201, 202, …).- A failure becomes a ProblemDetails whose status comes from the category, with
type,detailand thecodeextension.InvalidwithFieldsgives a ValidationProblemDetails. ASP.NET addstraceId(AddProblemDetails). - MVC controllers use
ToActionResult()and keep theirActionResult<T>return type (the OpenAPI document keeps the success body):public async Task<ActionResult<Run>> Get(...) => (await service.Get(...)).ToActionResult();. Returning theIResultofToHttpResult()from a controller loses the validationerrors(the MVC problem writer serializes the baseProblemDetailstype).error.ToProblem()anderror.ToActionResult()are also available on their own.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Elyspio.Utils.Results (>= 1.0.0)
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 | 82 | 11/15/2026 |
| 1.0.0-preview.3 | 59 | 11/15/2026 |
| 1.0.0-preview.2 | 59 | 11/15/2026 |
| 1.0.0-preview.1 | 53 | 11/15/2026 |