Resulver.AspNetCore.FastEndpoints 2.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Resulver.AspNetCore.FastEndpoints --version 2.0.4                
NuGet\Install-Package Resulver.AspNetCore.FastEndpoints -Version 2.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Resulver.AspNetCore.FastEndpoints" Version="2.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Resulver.AspNetCore.FastEndpoints --version 2.0.4                
#r "nuget: Resulver.AspNetCore.FastEndpoints, 2.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Resulver.AspNetCore.FastEndpoints as a Cake Addin
#addin nuget:?package=Resulver.AspNetCore.FastEndpoints&version=2.0.4

// Install Resulver.AspNetCore.FastEndpoints as a Cake Tool
#tool nuget:?package=Resulver.AspNetCore.FastEndpoints&version=2.0.4                

NuGet Package

Table of Contents


Overview

Resulver.AspNetCore.FastEndpoints is an extension of the Resulver library that integrates seamlessly with FastEndpoints. It simplifies structured error handling and result management, allowing you to focus on your business logic while the library takes care of consistent responses and error profiles.


Installation

To install the Resulver.AspNetCore.FastEndpoints package, use the following command:

dotnet add package Resulver.AspNetCore.FastEndpoints

Ensure you have the required .NET SDK installed.


Error Handling

1. Adding Resulver to Your Application

To enable Resulver in your application, add the following code to the Program.cs file:

builder.Services.AddResulver(Assembly.GetExecutingAssembly());

This registers all error profiles and ensures they are used when generating responses.

2. Creating Error Profiles

Error profiles define how specific error types should be translated into HTTP responses. For example:

  1. Define a custom error class:

    public class ValidationError(string title, string message) : ResultError(message, title: title);
    
  2. Create an error profile for the custom error:

    public class ValidationErrorProfile : ErrorProfile
    {
        public override void Configure()
        {
            AddError<ValidationError>().WithStatusCode(400);
        }
    }
    

With this profile, any ValidationError returned in a result will automatically generate a 400 Bad Request response.


Using in Endpoints

Method 1: Inheritance from the ResultBaseEndpoint Class

This approach simplifies result handling by using the ResultBaseEndpoint base class:

public class MyEndpoints : ResultBaseEndpoint<string, string>
{
    public override void Configure()
    {
        Post("my-endpoint");
        AllowAnonymous();
    }

    public override Task HandleAsync(string req, CancellationToken ct)
    {
        // Logic
        var result = new Result<string>("this is result message");

        // Return a response generated from the result
        return SendFromResultAsync(result, 200, ct);
    }
}

Note: If the Result contains errors, the response will automatically be generated based on the error profile defined for those errors.

Method 2: Inject IErrorResponseGenerator into Your Endpoint

For greater flexibility, you can inject IErrorResponseGenerator to manually handle errors:

public class MyEndpoints : Ep.Req<string>.Res<string>
{
    public required IErrorResponseGenerator<FailureResponse> ErrorResponseGenerator { get; init; }

    public override void Configure()
    {
        Post("my-endpoint");
        AllowAnonymous();
    }

    public override Task HandleAsync(string req, CancellationToken ct)
    {
        // Logic
        var result = new Result<string>("this is result message");

        if (result.IsFailure)
        {
            // Generate a failure response for the first error
            var failureResponse = ErrorResponseGenerator.MakeResponse(result.Errors[0]);
            AddError(failureResponse);

            // Send the error response
            return SendErrorsAsync(failureResponse.StatusCode, ct);
        }

        return SendOkAsync(result.Message, ct);
    }
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.