Resulver.AspNetCore.WebApi 1.2.4

Suggested Alternatives

Resulver.AspNetCore.Controllers

Additional Details

The 'Resulver.AspNetCore.WebApi' package has been deprecated and will no longer receive updates. Please switch to 'Resulver.AspNetCore.Controllers', which offers enhanced features and full support. For details on migrating to the new package, refer to the Resulver.AspNetCore.Controllers documentation.

There is a newer version of this package available.
See the version list below for details.
dotnet add package Resulver.AspNetCore.WebApi --version 1.2.4                
NuGet\Install-Package Resulver.AspNetCore.WebApi -Version 1.2.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.WebApi" Version="1.2.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Resulver.AspNetCore.WebApi --version 1.2.4                
#r "nuget: Resulver.AspNetCore.WebApi, 1.2.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.WebApi as a Cake Addin
#addin nuget:?package=Resulver.AspNetCore.WebApi&version=1.2.4

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

NuGet Package

Table of content

Installation

dotnet add package Resulver.AspNetCore.WebApi

Error Handling

1. First, add the following code to the Program.cs file:

builder.Services.AddErrorProfilesFromAssembly(Assembly.GetExecutingAssembly());
builder.Services.AddErrorResponseGenerator();

2. Create Error Profiles

In this example, we have created an error named ValidationError :

public class ValidationError(string title, string message) : ResultError(message, title: title)
{

}

Now, to manage responses related to this error, we will create a profile for it.

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

Additionally, to customize the responses, you can use the HandleWith() method :

public class ValidationErrorProfile : ErrorProfile
{
    public ValidationErrorProfile()
    {
        AddError<ValidationError>()
            .HandleWith(error =>
            {
                var response = new
                {
                    errorName = "validation problem",
                    errorMessage = error.Message
                };

                return new ObjectResult(response) { StatusCode = 400 };
            });
    }
}

Using in Controllers

You can use the following methods to utilize IResultHandler in your Controllers :

Method 1: Inheritance from the ResultBaseController class :
[ApiController]
[Route("api/[controller]")]
public class MyController : ResultBaseController
{
    [HttpGet]
    public IActionResult AddUser()
    {
        // logic
        //
        //

        var result = new Result("User Added");

        //use default 200 status code
        return FromResult(result);

        //or
        //use custom status code
        return FromResult(result, 200);

        //or
        //use customized result
        return FromResult(result, () => CreatedAtRoute("ExampleRouteName", result.Message));
    }
}

Note: In all cases, if the Result contains an error, a response will be generated based on the error profile created for that error.

Method 2: Inject IErrorResponseGenerator in to your controller:
[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
    readonly IErrorResponseGenerator _errorResponseGenerator;

    public MyController(IErrorResponseGenerator errorResponseGenerator)
    {
        _errorResponseGenerator = errorResponseGenerator;
    }

    [HttpGet]
    public IActionResult AddUser()
    {
        // logic
        //
        //

        var result = new Result("User Added");

        if (result.IsFailure)
        {
            return _errorResponseGenerator.MakeResponse(result.Errors[0]);
        }

        else return Ok(result.Message);
    }
}
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. 
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.

Version Downloads Last updated
1.2.5 108 10/13/2024 1.2.5 is deprecated.
1.2.4 157 9/15/2024 1.2.4 is deprecated.