CSharpFunctionalExtensions.HttpResults
0.0.2
See the version list below for details.
dotnet add package CSharpFunctionalExtensions.HttpResults --version 0.0.2
NuGet\Install-Package CSharpFunctionalExtensions.HttpResults -Version 0.0.2
<PackageReference Include="CSharpFunctionalExtensions.HttpResults" Version="0.0.2" />
<PackageVersion Include="CSharpFunctionalExtensions.HttpResults" Version="0.0.2" />
<PackageReference Include="CSharpFunctionalExtensions.HttpResults" />
paket add CSharpFunctionalExtensions.HttpResults --version 0.0.2
#r "nuget: CSharpFunctionalExtensions.HttpResults, 0.0.2"
#addin nuget:?package=CSharpFunctionalExtensions.HttpResults&version=0.0.2
#tool nuget:?package=CSharpFunctionalExtensions.HttpResults&version=0.0.2
CSharpFunctionalExtensions.HttpResults
Extensions for CSharpFunctionalExtensions to map Results to HttpResults in your MinimalApi
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 types to HttpResults
:
Result
Result<T>
Result<T,E>
UnitResult<E>
These methods are available:
Method | Short Description |
---|---|
.ToHttpResult() |
Returns StatusCodeHttpResult or ProblemHttpResult |
.ToHttpResult<T>() |
Returns JsonHttpResult<T> or ProblemHttpResult |
.ToHttpResult<T,E>() |
Returns JsonHttpResult<T> or custom error |
.ToNoContentHttpResult<T>() |
Discards value of Result<T> and returns empty StatusCodeHttpResult or ProblemHttpResult |
.ToNoContentHttpResult<T,E>() |
Discards value of Result<T> and returns empty StatusCodeHttpResult 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 |
.ToFileHttpResult<Stream>() |
Returns FileStreamHttpResult or ProblemHttpResult |
.ToFileHttpResult<Stream,E>() |
Returns FileStreamHttpResult or custom error |
.ToFileStreamHttpResult<Stream>() |
Returns FileStreamHttpResult or ProblemHttpResult |
.ToFileStreamHttpResult<Stream,E>() |
Returns FileStreamHttpResult or custom error |
For almost every method you can override the default status codes for Success/Failure.
All methods are available in sync and async variants.
By default, failures get mapped to a ProblemHttpResult
based on RFC9457.
The status property contains the status code.
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 property of the Result
.
If you want your own mapping logic read on.
Custom errors
This library uses a Source Generator to generate extension methods for your own custom error types when using Result<T,E>
or UnitResult<E>
.
- First create a custom error type that implements
IResultError
public class UserNotFoundError : IResultError { public required string UserId { get; init; } }
- Create a mapper that implements
IResultErrorMapper
which maps this custom error type to another type that you want to return in your web api:public class UserNotFoundErrorMapper : IResultErrorMapper<UserNotFoundError, Microsoft.AspNetCore.Http.IResult> { public Func<UserNotFoundErrorMapper, Microsoft.AspNetCore.Http.IResult> 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 generated extension method:
app.MapGet("/users/{id}", (string id) => { return userRepository.find(id) //Result<User,UserNotFoundError> .ToHttpResult(); });
Make sure that every IResult
implementation only has exactly one corresponding IResultMapper
implementation.
Optionally, you can use the ProblemDetailsMap.Find()
method to find a title and type for a status code based on RFC9110.
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 was computed. 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. |
-
net8.0
- CSharpFunctionalExtensions (>= 2.42.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 | 677 | 3/26/2025 |
0.9.5 | 475 | 3/26/2025 |
0.9.4 | 150 | 3/19/2025 |
0.9.3 | 148 | 3/19/2025 |
0.9.2 | 144 | 3/19/2025 |
0.9.1 | 193 | 3/12/2025 |
0.9.0 | 164 | 3/11/2025 |
0.8.0 | 139 | 2/11/2025 |
0.7.0 | 121 | 2/11/2025 |
0.6.0 | 113 | 2/8/2025 |
0.5.0 | 223 | 1/2/2025 |
0.4.0 | 116 | 1/2/2025 |
0.3.2 | 118 | 1/2/2025 |
0.3.1 | 104 | 1/2/2025 |
0.3.0 | 113 | 1/2/2025 |
0.2.0 | 2,199 | 7/18/2024 |
0.1.0 | 107 | 7/18/2024 |
0.0.5 | 119 | 7/12/2024 |
0.0.4 | 106 | 7/12/2024 |
0.0.3 | 103 | 7/12/2024 |
0.0.2 | 115 | 6/17/2024 |
0.0.1 | 117 | 6/14/2024 |