Seamlessly map Results from CSharpFunctionalExtensions to HttpResults for cleaner, more fluent Web APIs
$ dotnet add package CSharpFunctionalExtensions.HttpResultsSeamlessly map Results from CSharpFunctionalExtensions to HttpResults for cleaner, more fluent Web APIs
This library provides convenient extension methods to seamlessly map Results from CSharpFunctionalExtensions to HttpResults. With this, it streamlines your Web API resulting in cleaner, more fluent code.
Ok, Created, NoContent, Accepted, FileStream, and more.TypedResults for consistent, type-safe API responses.ProblemDetails), ensuring your API errors are standardized and interoperable.Available on NuGet.
dotnet add package CSharpFunctionalExtensions.HttpResults
or
PM> Install-Package CSharpFunctionalExtensions.HttpResults
[!NOTE] This library references an older version of CSharpFunctionalExtensions for wider compatibility. It's recommended to additionally install the latest version of CSharpFunctionalExtensions in your project to get the latest features and fixes.
This library provides you extension methods to map the following Result types to HttpResults at the end of our result chain:
ResultResult<T>Result<T,E>UnitResult<E>Example:
app.MapGet("/books", (BookService service) =>
service.Get() //Result<Book[]>
.ToOkHttpResult() //Results<Ok<Book[]>, ProblemHttpResult>
);
| Method | Short Description |
|---|---|
.ToStatusCodeHttpResult() | Returns StatusCodeHttpResult or ProblemHttpResult |
.ToStatusCodeHttpResult<T>() | Returns StatusCodeHttpResult or ProblemHttpResult |
.ToStatusCodeHttpResult<T,E>() | Returns StatusCodeHttpResult or custom error |
.ToStatusCodeHttpResult<E>() | Returns StatusCodeHttpResult or custom error |
.ToJsonHttpResult<T>() | Returns JsonHttpResult<T> or ProblemHttpResult |
.ToJsonHttpResult<T,E>() | Returns JsonHttpResult<T> or custom error |
.ToOkHttpResult<T>() | Returns Ok<T> or ProblemHttpResult |
.ToOkHttpResult<T,E>() | Returns Ok<T> or custom error |
.ToNoContentHttpResult() | Returns NoContent or ProblemHttpResult |
.ToNoContentHttpResult<T>() | Discards value of Result<T> and returns NoContent or ProblemHttpResult |
.ToNoContentHttpResult<T,E>() | Discards value of Result<T> and returns NoContent or custom error |
.ToNoContentHttpResult<E>() | Returns NoContent or custom error |
.ToCreatedHttpResult<T>() | Returns Created<T> or ProblemHttpResult |
.ToCreatedHttpResult<T,E>() | Returns Created<T> or custom error |
.ToCreatedAtRouteHttpResult<T>() | Returns CreatedAtRoute<T> or ProblemHttpResult |
.ToCreatedAtRouteHttpResult<T,E>() | Returns CreatedAtRoute<T> or custom error |
.ToAcceptedHttpResult<T>() | Returns Accepted<T> or ProblemHttpResult |
.ToAcceptedHttpResult<T,E>() | Returns Accepted<T> or custom error |
.ToAcceptedAtRouteHttpResult<T>() | Returns AcceptedAtRoute<T> or ProblemHttpResult |
.ToAcceptedAtRouteHttpResult<T,E>() | Returns AcceptedAtRoute<T> or custom error |
.ToFileHttpResult<byte[]>() | Returns FileContentHttpResult or ProblemHttpResult |
.ToFileHttpResult<byte[],E>() | Returns FileContentHttpResult or custom error |
.ToFileStreamHttpResult<Stream>() | Returns FileStreamHttpResult or ProblemHttpResult |
.ToFileStreamHttpResult<Stream,E>() | Returns FileStreamHttpResult or custom error |
.ToContentHttpResult<string>() | Returns ContentHttpResult or ProblemHttpResult |
.ToContentHttpResult<string,E>() | Returns ContentHttpResult or custom error |
.ToServerSentEventsHttpResult<IAsyncEnumerable<T>>() | Returns ServerSentEventsResult<T> or ProblemHttpResult (requires >= .NET 10) |
.ToServerSentEventsHttpResult<IAsyncEnumerable<T>,E>() | Returns ServerSentEventsResult<T> or custom error (requires >= .NET 10) |
All methods are available in sync and async variants.
By default, Result and Result<T> failures are mapped to a ProblemHttpResult based on RFC9457.
status property contains the status code of the HTTP response. Note: For almost every method you can override the default status codes for Success/Failure case.type property contains a URI to the corresponding RFC9110 entry based on the status code.title property contains a generic short messages based on the status code.detail property contains the error string of the Result.This default mapping behaviour is configured inside the ProblemDetailsMappingProvider.
You can override this behavior by providing your own dictionary that maps status codes to their corresponding title and type of the resulting ProblemDetails object.
ProblemDetailsMappingProvider.DefaultMappings = new Dictionary<int, (string? Title, string? Type)>
{
{ 400, ("Ungültige Anfrage", "https://tools.ietf.org/html/rfc9110#section-15.5.1") },
{ 401, ("Nicht autorisiert", "https://tools.ietf.org/html/rfc9110#section-15.5.2") },
{ 403, ("Verboten", "https://tools.ietf.org/html/rfc9110#section-15.5.4") },
{ 404, ("Nicht gefunden", "https://tools.ietf.org/html/rfc9110#section-15.5.5") },
{ 405, ("Methode nicht erlaubt", "https://tools.ietf.org/html/rfc9110#section-15.5.6") },
{ 406, ("Nicht akzeptabel", "https://tools.ietf.org/html/rfc9110#section-15.5.7") },
{ 408, ("Zeitüberschreitung der Anfrage", "https://tools.ietf.org/html/rfc9110#section-15.5.9") },
{ 409, ("Konflikt", "https://tools.ietf.org/html/rfc9110#section-15.5.10") },
{ 412, ("Vorbedingung fehlgeschlagen", "https://tools.ietf.org/html/rfc9110#section-15.5.13") },
{ 415, ("Nicht unterstützter Medientyp", "https://tools.ietf.org/html/rfc9110#section-15.5.16") },
{ 422, ("Nicht verarbeitbare Entität", "https://tools.ietf.org/html/rfc4918#section-11.2") },
{ 426, ("Upgrade erforderlich", "https://tools.ietf.org/html/rfc9110#section-15.5.22") },
{ 500, ("Ein Fehler ist bei der Verarbeitung Ihrer Anfrage aufgetreten.", "https://tools.ietf.org/html/rfc9110#section-15.6.1") },
{ 502, ("Schlechtes Gateway", "https://tools.ietf.org/html/rfc9110#section-15.6.3") },
{ 503, ("Dienst nicht verfügbar", "https://tools.ietf.org/html/rfc9110#section-15.6.4") },
{ 504, ("Gateway-Zeitüberschreitung", "https://tools.ietf.org/html/rfc9110#section-15.6.5") },
};
Example from here
You don't have to provide the whole dictionary; you can also override or add mappings for specific status codes like this:
ProblemDetailsMappingProvider.AddOrUpdateMapping(420, "Enhance Your Calm", "https://http-status-code.de/420/");
It's recommended to override the mappings during startup e.g. in Program.cs.
If you need to override the mapping for a specific use case in a single location, you can provide an Action<ProblemDetails> to fully customize the ProblemDetails. This is particularly useful when you want to add extensions or tailor the ProblemDetails specifically for that use case.
...
.ToOkHttpResult(customizeProblemDetails: problemDetails =>
{
problemDetails.Title = "Custom Title";
problemDetails.Extensions.Add("custom", "value");
});
When using Result<T,E> or UnitResult<E>, this library uses a Source Generator to generate extension methods for your own custom error types.
public record UserNotFoundError(string UserId);
IResultErrorMapper which maps this custom error type to an HttpResult / Microsoft.AspNetCore.Http.IResult that you want to return in your Web API:
public class UserNotFoundErrorMapper : IResultErrorMapper<UserNotFoundError, ProblemHttpResult>
{
public ProblemHttpResult Map(UserNotFoundError error)
{
var problemDetails = new ProblemDetails
{
Status = 404,
Title = "User not found",
Type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
Detail = $"The user with ID {error.UserId} couldn't be found.
};
return TypedResults.Problem(problemDetails);
};
}
app.MapGet("/users/{id}", (string id, UserRepository repo) =>
repo.Find(id) //Result<User,UserNotFoundError>
.ToOkHttpResult() //Results<Ok<User>,ProblemHttpResult>
);
[!IMPORTANT]
Make sure that each custom error type has exactly one correspondingIResultMapperimplementation.
[!TIP] You can use the
ProblemDetailsMappingProvider.FindMapping()method to find a suitable title and type for a status code based on RFC9110.
[!NOTE] If extension methods for custom errors are missing, rebuild the project to trigger Source Generation.
This library includes analyzers to help you use it correctly.
For example, they will notify you if you have multiple mappers for the same custom error type or if your mapper class doesn't have a parameterless constructor.
You can find a complete list of all analyzers here.
The CSharpFunctionalExtensions.HttpResults.Examples project contains various examples demonstrating how to use this library in different scenarios, including:
GET, POST, PUT, and DELETE requestsCheck out the example project for hands-on implementation details!
Contributions are welcome! Please keep the following rules in mind:
This project uses CSharpier for code formatting. You can format your code with dotnet csharpier ..
To add new methods follow these steps:
Result and Result<T> to CSharpFunctionalExtensions.HttpResults.ResultExtensionsResult<T,E> to CSharpFunctionalExtensions.HttpResults.Generators.ResultExtensions and add the class to ResultExtensionsClassBuilderUnitResult<E> to CSharpFunctionalExtensions.HttpResults.Generators.UnitResultExtensions and add the class to UnitResultExtensionsClassBuilderCSharpFunctionalExtensions.HttpResults.Tests