Bodatero.Result.AspNetCore
1.0.1
dotnet add package Bodatero.Result.AspNetCore --version 1.0.1
NuGet\Install-Package Bodatero.Result.AspNetCore -Version 1.0.1
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="Bodatero.Result.AspNetCore" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bodatero.Result.AspNetCore" Version="1.0.1" />
<PackageReference Include="Bodatero.Result.AspNetCore" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bodatero.Result.AspNetCore --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Bodatero.Result.AspNetCore, 1.0.1"
#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.
#addin nuget:?package=Bodatero.Result.AspNetCore&version=1.0.1
#tool nuget:?package=Bodatero.Result.AspNetCore&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Bodatero.Result.AspNetCore
Overview
Bodatero.Result.AspNetCore is an extension for ASP.NET Core Minimal APIs that simplifies handling endpoint responses using the Result<T>
pattern. Instead of throwing exceptions for validation errors or failures, this approach ensures a structured and predictable API response handling mechanism.
Why Use Result<T> Instead of Throwing Exceptions?
- Performance: Throwing exceptions is costly in terms of performance, while returning
Result<T>
avoids unnecessary stack unwinding. - Predictability: API responses become more structured and avoid unexpected 500 errors.
- Customizability: Developers can define custom success and failure handlers, improving flexibility in API design.
- Better Client Experience: Clients receive consistent response structures, making error handling easier.
Installation
To use Bodatero.Result.AspNetCore
, install it via NuGet:
dotnet add package Bodatero.Result.AspNetCore
Usage
1. Register the Result Service
Modify Program.cs
to register the ResultService
:
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.AddResult();
// Optionally configure the service
//services.AddResult(cfg =>
//{
// cfg.SuccessResultHandler = (context, value) => new OkResponse(value);
// cfg.FailResultHandler = (context, error) => new BadResponse(error);
//});
var app = builder.Build();
2. Using the Service in Minimal API Endpoints
Example 1: Using WithResult<T>
for a Specific Endpoint
var apiGroup = app.MapGroup("api/products");
apiGroup.MapPost("", ([FromBody] CreateProductRequest createProductRequest) =>
{
Result<Product> CreateProduct()
{
List<string> validationErrors = new();
if (string.IsNullOrEmpty(createProductRequest.Name))
{
validationErrors.Add("Name must not be empty");
}
if (string.IsNullOrEmpty(createProductRequest.Description))
{
validationErrors.Add("Description must not be empty");
}
return validationErrors.Any()
? new ValidationException(string.Join('\n', validationErrors))
: new Product { Name = createProductRequest.Name, Description = createProductRequest.Description };
}
return CreateProduct();
}).WithResult<Product>();
Example 2: Apply WithResult
to an Entire Route Group
var apiGroup = app.MapGroup("api/products").WithResult(); // Applied to whole group
apiGroup.MapDelete("{id}", (Guid id) =>
{
// return productService.Delete(id);
}); // `WithResult` handler will be executed
Example 3: Custom Result Handler for a Specific Endpoint
apiGroup.MapDelete("{id}", (Guid id) =>
{
// return productService.Delete(id);
})
.WithResult(new ResultServiceConfig(
(context, value) => new OkResponse(value), // Custom success handler
(context, error) => new BadResponse(error) // Custom error handler
));
Please support me with a donation on Patreon
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Bodatero.Result (>= 1.0.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.