CSharpFunctionalExtensions.HttpResults
0.0.1
See the version list below for details.
dotnet add package CSharpFunctionalExtensions.HttpResults --version 0.0.1
NuGet\Install-Package CSharpFunctionalExtensions.HttpResults -Version 0.0.1
<PackageReference Include="CSharpFunctionalExtensions.HttpResults" Version="0.0.1" />
paket add CSharpFunctionalExtensions.HttpResults --version 0.0.1
#r "nuget: CSharpFunctionalExtensions.HttpResults, 0.0.1"
// Install CSharpFunctionalExtensions.HttpResults as a Cake Addin #addin nuget:?package=CSharpFunctionalExtensions.HttpResults&version=0.0.1 // Install CSharpFunctionalExtensions.HttpResults as a Cake Tool #tool nuget:?package=CSharpFunctionalExtensions.HttpResults&version=0.0.1
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 | Description |
---|---|
.ToHttpResult() |
|
.ToNoContentHttpResult() |
Discards Result value and returns empty response |
.ToCreatedHttpResult() |
|
.ToCreatedAtRouteHttpResult() |
|
.ToAcceptedHttpResult() |
|
.ToAcceptedAtRouteHttpResult() |
|
.ToFileHttpResult() |
|
.ToFileStreamHttpResult() |
For almost every method you can override the default status codes for Success/Failure by passing corresponding int
values.
By default, failures get mapped to a ProblemHttpResult
.
If you want your own mapping logic or even don't want to map to HttpErrors
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.
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. |
-
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.