https://github.com/co-it/csharpfunctionalextensions.httpresults
Extensions for CSharpFunctionalExtensions to map Results to HttpResults in your Web-API
https://github.com/co-it/csharpfunctionalextensions.httpresults
actionresult api csharp csharpfunctionalextensions dotnet http minimal nuget result
Last synced: 3 months ago
JSON representation
Extensions for CSharpFunctionalExtensions to map Results to HttpResults in your Web-API
- Host: GitHub
- URL: https://github.com/co-it/csharpfunctionalextensions.httpresults
- Owner: co-IT
- License: mit
- Created: 2024-06-13T15:29:56.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-19T13:28:43.000Z (4 months ago)
- Last Synced: 2025-03-19T13:47:30.133Z (4 months ago)
- Topics: actionresult, api, csharp, csharpfunctionalextensions, dotnet, http, minimal, nuget, result
- Language: C#
- Homepage: https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults
- Size: 146 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# CSharpFunctionalExtensions.HttpResults
[](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/)
[](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/)
[](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/)
[](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/LICENSE.md)Seamlessly map Results from [CSharpFunctionalExtensions](https://github.com/vkhorikov/CSharpFunctionalExtensions) to HttpResults for cleaner, more fluent Web APIs
Table of Contents
1. [Overview](#overview)
2. [Installation](#installation)
3. [Usage](#usage)
1. [Available methods](#available-methods)
2. [Default mapping](#default-mapping)
3. [Custom error mapping](#custom-error-mapping)
4. [Analyzers](#analyzers)
5. [Examples](#examples)
6. [Development](#development)## Overview
This library provides convenient extension methods to seamlessly map Results from [CSharpFunctionalExtensions](https://github.com/vkhorikov/CSharpFunctionalExtensions) to HttpResults. With this, it streamlines your Web API resulting in cleaner, more fluent code.
### Key Benefits
- ⚙️ **Zero Configuration:** Get started immediately — the mapping works out of the box without any configuration.
- 🛠️ **Customizable Mappings:** Tailor default mappings or define custom mappings for specific use cases.
- 🔗 **Fluent API:** Maintain a smooth, railway-oriented flow by chaining HttpResult mappings at the end of your Result chain.
- 🧱 **Separation of Domain and HTTP Errors:** Keeps domain errors distinct from HTTP errors, improving maintainability and clarity between business logic and web API concerns.
- ⚡ **Minimal APIs & Controllers Support:** Works with both Minimal APIs and traditional controllers in ASP.NET.
- 📦 **Full Support for ASP.NET Results:** Supports all built-in HTTP response types in ASP.NET, including `Ok`, `Created`, `NoContent`, `Accepted`, `FileStream`, and more.
- 🦺 **Typed Results:** Utilizes `TypedResults` for consistent, type-safe API responses.
- 📑 **OpenAPI Ready:** Ensures accurate OpenAPI generation for clear and reliable API documentation.
- 🛡️ **RFC Compliance:** Default mappings adhere to the RFC 9457 standard (`ProblemDetails`), ensuring your API errors are standardized and interoperable.
- 🧑💻 **Developer-Friendly:** Includes built-in analyzers and source generators to speed up development and reduce errors.## Installation
Available on [NuGet](https://www.nuget.org/packages/CSharpFunctionalExtensions.HttpResults/).
```bash
dotnet add package CSharpFunctionalExtensions.HttpResults
```or
```powershell
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.## Usage
This library provides you extension methods to map the following `Result` types to `HttpResults` at the end of our result chain:
- `Result`
- `Result`
- `Result`
- `UnitResult`*Example:*
```csharp
app.MapGet("/books", (BookService service) =>
service.Get() //Result
.ToOkHttpResult() //Results, ProblemHttpResult>
);
```### Available methods
Click here to view all available methods.
| Method | Short Description |
|---------------------------------------|------------------------------------------------------------------------------|
| `.ToStatusCodeHttpResult()` | Returns `StatusCodeHttpResult` or `ProblemHttpResult` |
| `.ToStatusCodeHttpResult()` | Returns `StatusCodeHttpResult` or `ProblemHttpResult` |
| `.ToStatusCodeHttpResult()` | Returns `StatusCodeHttpResult` or custom error |
| `.ToStatusCodeHttpResult()` | Returns `StatusCodeHttpResult` or custom error |
| `.ToJsonHttpResult()` | Returns `JsonHttpResult` or `ProblemHttpResult` |
| `.ToJsonHttpResult()` | Returns `JsonHttpResult` or custom error |
| `.ToOkHttpResult()` | Returns `Ok` or `ProblemHttpResult` |
| `.ToOkHttpResult()` | Returns `Ok` or custom error |
| `.ToNoContentHttpResult()` | Returns `NoContent` or `ProblemHttpResult` |
| `.ToNoContentHttpResult()` | Discards value of `Result` and returns `NoContent` or `ProblemHttpResult` |
| `.ToNoContentHttpResult()` | Discards value of `Result` and returns `NoContent` or custom error |
| `.ToNoContentHttpResult()` | Returns `NoContent` or custom error |
| `.ToCreatedHttpResult()` | Returns `Created` or `ProblemHttpResult` |
| `.ToCreatedHttpResult()` | Returns `Created` or custom error |
| `.ToCreatedAtRouteHttpResult()` | Returns `CreatedAtRoute` or `ProblemHttpResult` |
| `.ToCreatedAtRouteHttpResult()` | Returns `CreatedAtRoute` or custom error |
| `.ToAcceptedHttpResult()` | Returns `Accepted` or `ProblemHttpResult` |
| `.ToAcceptedHttpResult()` | Returns `Accepted` or custom error |
| `.ToAcceptedAtRouteHttpResult()` | Returns `AcceptedAtRoute` or `ProblemHttpResult` |
| `.ToAcceptedAtRouteHttpResult()` | Returns `AcceptedAtRoute` or custom error |
| `.ToFileHttpResult()` | Returns `FileContentHttpResult` or `ProblemHttpResult` |
| `.ToFileHttpResult()` | Returns `FileContentHttpResult` or custom error |
| `.ToFileStreamHttpResult()` | Returns `FileStreamHttpResult` or `ProblemHttpResult` |
| `.ToFileStreamHttpResult()` | Returns `FileStreamHttpResult` or custom error |
| `.ToContentHttpResult()` | Returns `ContentHttpResult` or `ProblemHttpResult` |
| `.ToContentHttpResult()` | Returns `ContentHttpResult` or custom error |All methods are available in sync and async variants.
### Default mapping
By default, `Result` and `Result` failures are mapped to a `ProblemHttpResult` based on [RFC9457](https://www.rfc-editor.org/rfc/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](https://tools.ietf.org/html/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 the `Result`.This default mapping behaviour is configured inside the [`ProblemDetailsMappingProvider`](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults/ProblemDetailsMappingProvider.cs).
#### Override default mapping
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.
Click here to see an example of changing the default mapping for German localization.
```csharp
ProblemDetailsMappingProvider.DefaultMappings = new Dictionary
{
{ 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](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Program.cs#L9-L34)
You don't have to provide the whole dictionary; you can also override or add mappings for specific status codes like this:
```csharp
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` to fully customize the `ProblemDetails`. This is particularly useful when you want to add extensions or tailor the `ProblemDetails` specifically for that use case.
```csharp
...
.ToOkHttpResult(customizeProblemDetails: problemDetails =>
{
problemDetails.Title = "Custom Title";
problemDetails.Extensions.Add("custom", "value");
});
```### Custom error mapping
When using `Result` or `UnitResult`, this library uses a Source Generator to generate extension methods for your own custom error types.
1. Create a custom error type
```csharp
public record UserNotFoundError(string UserId);
```
2. Create a mapper that implements `IResultErrorMapper` which maps this custom error type to an HttpResult / `Microsoft.AspNetCore.Http.IResult` that you want to return in your Web API:
```csharp
public class UserNotFoundErrorMapper : IResultErrorMapper
{
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);
};
}
```
3. Use the auto generated extension method:
```csharp
app.MapGet("/users/{id}", (string id, UserRepository repo) =>
repo.Find(id) //Result
.ToOkHttpResult() //Results,ProblemHttpResult>
);
```> [!IMPORTANT]
> Make sure that each custom error type has exactly one corresponding `IResultMapper` implementation.> [!TIP]
> You can use the `ProblemDetailsMappingProvider.FindMapping()` method to find a suitable title and type for a status code based on [RFC9110](https://tools.ietf.org/html/rfc9110).> [!NOTE]
> 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 will notify you if you have multiple mappers for the same custom error type.
You can find a complete list of all analyzers [here](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Generators/AnalyzerReleases.Shipped.md).
## Examples
The [`CSharpFunctionalExtensions.HttpResults.Examples`](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples) project contains various examples demonstrating how to use this library in different scenarios, including:
- **[Basic CRUD operations](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/CRUD)** – Handling `GET`, `POST`, `PUT`, and `DELETE` requests
- **[File handling](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/FileStream)** – Returning files from your Web API
- **[Custom error mapping](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/CustomError)** – Defining and mapping custom error types to meaningful HTTP responses
- **[Multiple errors in chain](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Features/MultipleErrorChain)** – Using different kind of custom errors in the same result chain
- **[Customizing default mapping](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/CSharpFunctionalExtensions.HttpResults.Examples/Program.cs)** – Overriding default mappings for localization or specific use casesCheck out the example project for hands-on implementation details!
## Development
Contributions are welcome! Please keep the following 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 somethingThis project uses [CSharpier](https://csharpier.com) for code formatting. You can format your code with `dotnet csharpier .`.
### Add new extension methods
To add new methods follow these steps:
1. Add methods for `Result` and `Result` to `CSharpFunctionalExtensions.HttpResults.ResultExtensions`
2. Add methods for `Result` to `CSharpFunctionalExtensions.HttpResults.Generators.ResultExtensions` and add the class to `ResultExtensionsClassBuilder`
3. Add methods for `UnitResult` to `CSharpFunctionalExtensions.HttpResults.Generators.UnitResultExtensions` and add the class to `UnitResultExtensionsClassBuilder`
4. Add tests for **all** new methods to `CSharpFunctionalExtensions.HttpResults.Tests`
5. Add methods to [README](https://github.com/co-IT/CSharpFunctionalExtensions.HttpResults/blob/main/README.md)