CSharpFunctionalExtensions.HttpResults
0.8.0
dotnet add package CSharpFunctionalExtensions.HttpResults --version 0.8.0
NuGet\Install-Package CSharpFunctionalExtensions.HttpResults -Version 0.8.0
<PackageReference Include="CSharpFunctionalExtensions.HttpResults" Version="0.8.0" />
paket add CSharpFunctionalExtensions.HttpResults --version 0.8.0
#r "nuget: CSharpFunctionalExtensions.HttpResults, 0.8.0"
// Install CSharpFunctionalExtensions.HttpResults as a Cake Addin #addin nuget:?package=CSharpFunctionalExtensions.HttpResults&version=0.8.0 // Install CSharpFunctionalExtensions.HttpResults as a Cake Tool #tool nuget:?package=CSharpFunctionalExtensions.HttpResults&version=0.8.0
CSharpFunctionalExtensions.HttpResults
Extensions for CSharpFunctionalExtensions to map Results to HttpResults in your Web-API
Overview
This library streamlines returning HttpResults from endpoints that leverage CSharpFunctionalExtensions by offering convenient extension methods to map you result to an HttpResult. With these, you can maintain a fluent, railway-oriented style by simply invoking the appropriate method at the end of your result chain. It also supports custom error types and ensures a clear separation between your domain logic and API by using specific mappers to translate domain details into API responses.
Installation
Available on NuGet.
dotnet add package CSharpFunctionalExtensions.HttpResults
or
PM> Install-Package CSharpFunctionalExtensions.HttpResults
Usage
This library provides you extension methods to map the following Result
types to HttpResults
:
Result
Result<T>
Result<T,E>
UnitResult<E>
These methods are available:
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 |
All methods are available in sync and async variants.
Default mapping
By default, Result
and Result<T>
failures get mapped to a ProblemHttpResult
based on RFC9457.
- The
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. - The
type
property contains a URI to the corresponding RFC9110 entry based on the status code. - The
title
property contains a generic short messages based on the status code. - The
detail
property contains the error string of theResult
.
This default mapping behaviour is configured inside the ProblemDetailsMappingProvider
.
Override default mapping
You can override this behaviour by providing your own dictionary which maps status code to title and type of the resulting ProblemDetails
object.
<details> <summary>Example for changing the default mapping for german localization</summary>
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
</details>
You don't have to provide the whole dictionary but can also override or add only 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
.
Override mapping for single use case
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.
Result.Failure<string>(error)
.ToOkHttpResult(customizeProblemDetails: problemDetails =>
{
problemDetails.Title = "Custom Title";
problemDetails.Extensions.Add("custom", "value");
});
Custom error mapping
When using Result<T,E>
or UnitResult<E>
, this library uses a Source Generator to generate extension methods for your own custom error types.
- Create a custom error type
public record UserNotFoundError(string UserId);
- Create a mapper that implements
IResultErrorMapper
which maps this custom error type to an HttpResult that you want to return in your Web-API:public class UserNotFoundErrorMapper : IResultErrorMapper<UserNotFoundError, ProblemHttpResult> { public Func<UserNotFoundErrorMapper, ProblemHttpResult> Map => 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); }; }
- Use the auto generated extension method:
app.MapGet("/users/{id}", (string id) => { return userRepository.Find(id) //Result<User,UserNotFoundError> .ToOkHttpResult(); //Results<Ok<User>,ProblemHttpResult> });
[!IMPORTANT]
Make sure that every custom error type has exactly one correspondingIResultMapper
implementation.
You can use the ProblemDetailsMappingProvider.FindMapping()
method to find a suitable title and type for a status code based on RFC9110.
[!TIP] If extension methods for custom errors are missing, rebuild the project to trigger Source Generation.
Analyzers
This library includes analyzers to help you use it correctly.
For example, they can notify you if you have multiple mappers for the same custom error type or if you forgot to implement the Map
function in your custom error mapper.
You can find a complete list of all analyzers here.
Examples
Examples for CRUD, FileStreams, custom errors, etc. in context of a Web-API are available in the CSharpFunctionalExtensions.HttpResults.Examples
project.
Development
You're welcome to contribute. Please keep these few rules in mind:
- add documentation in the form of summary comments
- add tests for your additions
- add sync and async variants where possible
- refer to existing code files and the folder structure when adding something
This project uses CSharpier for code formatting. You can format your code with dotnet csharpier .
.
Add new extension methods
To add new methods follow these steps:
- Add methods for
Result
andResult<T>
toCSharpFunctionalExtensions.HttpResults.ResultExtensions
- Add methods for
Result<T,E>
toCSharpFunctionalExtensions.HttpResults.Generators.ResultExtensions
and add the class toResultExtensionsClassBuilder
- Add methods for
UnitResult<E>
toCSharpFunctionalExtensions.HttpResults.Generators.UnitResultExtensions
and add the class toUnitResultExtensionsClassBuilder
- Add tests for all new methods to
CSharpFunctionalExtensions.HttpResults.Tests
- Add methods to README
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
-
net7.0
- CSharpFunctionalExtensions (>= 3.4.3)
-
net8.0
- CSharpFunctionalExtensions (>= 3.4.3)
-
net9.0
- CSharpFunctionalExtensions (>= 3.4.3)
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 |
---|---|---|
0.8.0 | 39 | 2/11/2025 |
0.7.0 | 35 | 2/11/2025 |
0.6.0 | 71 | 2/8/2025 |
0.5.0 | 121 | 1/2/2025 |
0.4.0 | 93 | 1/2/2025 |
0.3.2 | 91 | 1/2/2025 |
0.3.1 | 86 | 1/2/2025 |
0.3.0 | 90 | 1/2/2025 |
0.2.0 | 1,939 | 7/18/2024 |
0.1.0 | 94 | 7/18/2024 |
0.0.5 | 99 | 7/12/2024 |
0.0.4 | 95 | 7/12/2024 |
0.0.3 | 91 | 7/12/2024 |
0.0.2 | 105 | 6/17/2024 |
0.0.1 | 98 | 6/14/2024 |