ResultCrafter.AspNetCore
3.1.0
dotnet add package ResultCrafter.AspNetCore --version 3.1.0
NuGet\Install-Package ResultCrafter.AspNetCore -Version 3.1.0
<PackageReference Include="ResultCrafter.AspNetCore" Version="3.1.0" />
<PackageVersion Include="ResultCrafter.AspNetCore" Version="3.1.0" />
<PackageReference Include="ResultCrafter.AspNetCore" />
paket add ResultCrafter.AspNetCore --version 3.1.0
#r "nuget: ResultCrafter.AspNetCore, 3.1.0"
#:package ResultCrafter.AspNetCore@3.1.0
#addin nuget:?package=ResultCrafter.AspNetCore&version=3.1.0
#tool nuget:?package=ResultCrafter.AspNetCore&version=3.1.0
ResultCrafter.AspNetCore
Return errors. Don't throw them. Turn a Result<T> from your service into the right HTTP response in one call.
Errors leave as clean RFC 9457 ProblemDetails, the same shape every time. For .NET 10 and later.
Setup
using ResultCrafter.AspNetCore;
builder.Services.AddResultCrafter();
var app = builder.Build();
app.UseResultCrafter();
Use
The service returns what happened:
public async Task<Result<OrderDto>> GetAsync(int id, CancellationToken ct)
{
var order = await repo.FindAsync(id, ct);
if (order is null)
return Error.NotFound($"Order {id} does not exist.");
return order.ToDto();
}
The endpoint maps it in one call:
app.MapGet("/orders/{id:int}", async (int id, OrderService orders, CancellationToken ct) =>
(await orders.GetAsync(id, ct)).ToOkResult());
The client gets 200 with the order, or a 404 like this:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.5",
"title": "not_found",
"status": 404,
"detail": "Order 42 does not exist.",
"instance": "/orders/42",
"traceId": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00",
"requestId": "0HN8K2MJ7F4QP:00000001"
}
What you get
- Nine error types with the right status: 400, 401, 403, 404, 409 (conflict and concurrency conflict), 410, 422,
and 429 with
Retry-After. - One ProblemDetails shape for your errors, binding failures, built-in validation and unhandled exceptions.
- Field errors a client can translate: next to the
errorsmessages,errorCodescarries a code and args for each. - Typed success results (
200,201,202,204) that OpenAPI sees, and.ProducesNotFound()and friends for the errors. - Source-generated logging: 4xx at Warning, 5xx at Error.
- Minimal APIs first, MVC controllers supported (
ResultCrafter.AspNetCore.Mvc). - NativeAOT and trim compatible.
Learn more
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- ResultCrafter.Core (>= 3.1.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on ResultCrafter.AspNetCore:
| Package | Downloads |
|---|---|
|
ResultCrafter.AspNetCore.EfCore
EF Core integration for ResultCrafter ASP.NET Core. Maps an optimistic-concurrency DbUpdateConcurrencyException to a 409 RFC 9457 ProblemDetails via IExceptionHandler. Serialization failures and deadlocks (SQLSTATE 40001, 40P01) stay 500. |
|
|
ResultCrafter.AspNetCore.FluentValidation
FluentValidation endpoint-filter integration for ResultCrafter ASP.NET Core. Adds WithValidation<T>() to validate Minimal API requests with their registered IValidator<T> and short-circuit with an RFC 9457 ProblemDetails 400. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Added: field error codes, so a client can show validation errors in its own language. A validation 400 now carries errorCodes next to errors: the same keys, and for each message a { code, args } entry at the same index; errors keeps its exact shape, and errorCodes is left out when no field error has a code or args. New FieldError type (message, optional code, typed args through With) and Error.BadRequest(IReadOnlyDictionary<string, FieldError[]>) for field errors built in a handler; Error.FieldErrors reads them back. ResultCrafter.FluentValidation and WithValidation<T>() fill the code from each failure's ErrorCode (FluentValidation's validator name, such as NotEmptyValidator, when the rule sets none) and the rule's limits as args (minLength, maxLength when the rule has a maximum, from, to, expectedPrecision, expectedScale, and a comparisonValue that is neither a string nor another property); the attempted value is never copied. ProblemResponse documents errorCodes in OpenAPI (new FieldErrorCode schema type). Changed: two validation errors with the same messages but different codes or args are no longer equal. Error.BadRequest(IReadOnlyDictionary<string, string[]>) now throws ArgumentException for a null message instead of sending JSON null, which the errors schema does not allow. Update all ResultCrafter packages together: an older package next to a 3.1 one drops errorCodes and sends the 3.0 body. Guide: https://github.com/HaikAsatryan/ResultCrafter/blob/main/docs/guide.md#field-error-codes