Fluent.Unions.AspNetCore
1.1.0
dotnet add package Fluent.Unions.AspNetCore --version 1.1.0
NuGet\Install-Package Fluent.Unions.AspNetCore -Version 1.1.0
<PackageReference Include="Fluent.Unions.AspNetCore" Version="1.1.0" />
<PackageVersion Include="Fluent.Unions.AspNetCore" Version="1.1.0" />
<PackageReference Include="Fluent.Unions.AspNetCore" />
paket add Fluent.Unions.AspNetCore --version 1.1.0
#r "nuget: Fluent.Unions.AspNetCore, 1.1.0"
#:package Fluent.Unions.AspNetCore@1.1.0
#addin nuget:?package=Fluent.Unions.AspNetCore&version=1.1.0
#tool nuget:?package=Fluent.Unions.AspNetCore&version=1.1.0
![]()
Fluent.Unions.AspNetCore
Turns Result, Result<T> and Option<T> into HTTP responses, so an endpoint returns the outcome of an operation and the status code follows from it. It builds on Fluent.Unions.
Installation
dotnet add package Fluent.Unions.AspNetCore
Usage
Convert a result in the handler with ToHttpResult. A value gives 200, a succeeded Result gives 204, None gives 404, and an error gives ProblemDetails. Validation errors give 400 with a ValidationProblemDetails grouped by property.
using Fluent.Unions.AspNetCore;
app.MapGet("/orders/{id}", async (Guid id, IOrders orders) => await orders.LoadOrderAsync(id).ToHttpResult());
app.MapDelete("/orders/{id}", (Guid id, IOrders orders) => orders.CancelAsync(id).ToHttpResult());
Let handlers return results directly. WithResultMapping works on one endpoint or a whole route group, and publishes the value type and the error responses to OpenAPI.
var group = app.MapGroup("/v2/orders").WithResultMapping();
group.MapGet("/{id}", (Guid id, IOrders orders) => orders.LoadOrderAsync(id)); // Task<Result<Order>>
With your own result type
WithResultMapping handles any result type, also one declared with [FluentResult<TError>]: a value gives 200, a succeeded value-less result gives 204, and an error gives the configured error mapping. The OpenAPI metadata has the same shape.
group.MapGet("/customers/{id}", (Guid id) => GetCustomer(id)); // Shop.Result<Customer>
ToHttpResult() also accepts IResult<TSelf, T, TError> when TError : Exception, and maps like the default type. ToHttpResult(onError) works for any error type. There are no Task overloads for your own type, so await first. See your own result type.
By default ValidationError and ValidationErrors give 400, KeyNotFoundException 404, UnauthorizedAccessException 403 and TimeoutException 504. Everything else gives 500 and is logged at Error level. Only validation errors show their messages to the client, also when a ValidationError is the inner exception of your own error. Map the exception your FromException creates for unexpected failures to 500, because it otherwise inherits the mapping of your base exception. Map your own exception types, including their subclasses, and choose whether the message is safe to expose:
builder.Services.Configure<ResultHttpOptions>(o => o
.Map<OrderShippedException>(StatusCodes.Status409Conflict, exposeMessage: true)
.Map<UnexpectedDomainException>(StatusCodes.Status500InternalServerError));
The OpenAPI metadata of WithResultMapping lists 400 and 500 for errors only. Add .Produces(...) for the other statuses you map. A faulted Task is not converted: its exception goes through ASP.NET Core's exception handling.
Map errors that are data with onError. The switch makes the compiler check every case.
app.MapGet("/checkout/{id}", (int id) => Checkout(id).ToHttpResult(error => error switch
{
CustomerNotFound => TypedResults.NotFound(),
CartEmpty => TypedResults.BadRequest(),
Unexpected => TypedResults.StatusCode(500)
}));
Documentation
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Fluent.Unions (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.