Behrouzan.Results.AspNetCore
0.3.0
dotnet add package Behrouzan.Results.AspNetCore --version 0.3.0
NuGet\Install-Package Behrouzan.Results.AspNetCore -Version 0.3.0
<PackageReference Include="Behrouzan.Results.AspNetCore" Version="0.3.0" />
<PackageVersion Include="Behrouzan.Results.AspNetCore" Version="0.3.0" />
<PackageReference Include="Behrouzan.Results.AspNetCore" />
paket add Behrouzan.Results.AspNetCore --version 0.3.0
#r "nuget: Behrouzan.Results.AspNetCore, 0.3.0"
#:package Behrouzan.Results.AspNetCore@0.3.0
#addin nuget:?package=Behrouzan.Results.AspNetCore&version=0.3.0
#tool nuget:?package=Behrouzan.Results.AspNetCore&version=0.3.0
Behrouzan.Results.AspNetCore
ASP.NET Core integration for Behrouzan.Results.
This package converts application results into ASP.NET Core HTTP responses while keeping the core Result model independent from HTTP concerns.
Features
Result<T>toIResultconversion for Minimal APIsResult<T>toIActionResultconversion for controller-based APIs- Non-generic
Resultsupport - Automatic HTTP status code mapping
- Problem Details responses
- Structured error details
- Validation error support
- Trace identifiers
- Configurable HTTP status mappings
- Configurable problem type base
- Shared response contract for Minimal APIs and controller-based APIs
Installation
dotnet add package Behrouzan.Results.AspNetCore
Behrouzan.Results is installed automatically as a dependency.
Registration
Register the integration during application startup:
builder.Services.AddBehrouzanResultHttp();
Basic Usage
A Result<T> can be converted directly to an ASP.NET Core IResult:
app.MapGet("/products/{id:int}", (int id) =>
{
Result<Product> result = GetProduct(id);
return result.ToHttpResult();
});
A successful result returns the contained value.
For example:
{
"id": 1,
"name": "Laptop",
"price": 1500
}
Controller-based APIs
For ASP.NET Core controller-based APIs, convert a result to an IActionResult using ToActionResult().
[ApiController]
[Route("api/[controller]")]
public sealed class ProductsController : ControllerBase
{
private readonly ProductService _service;
public ProductsController(ProductService service)
{
_service = service;
}
[HttpGet("{id:int}")]
public IActionResult GetById(int id)
{
return _service
.GetById(id)
.ToActionResult();
}
}
Successful generic results return HTTP 200 with the contained value.
Failed results use the same Problem Details contract and HTTP status mapping as ToHttpResult().
Non-generic successful results return HTTP 204 No Content:
[HttpDelete("{id:int}")]
public IActionResult Delete(int id)
{
return _service
.Delete(id)
.ToActionResult();
}
Minimal API vs Controller API
Use:
result.ToHttpResult();
for Minimal APIs.
Use:
result.ToActionResult();
for controller-based APIs.
Both paths use the same error mapping, Problem Details format, configuration, and trace identifier behavior.
Failure Responses
Application errors are automatically converted into Problem Details responses.
For example:
return Result<Product>.Failure(
Error.NotFound(
"Product.NotFound",
"Product was not found."));
produces an HTTP 404 response similar to:
{
"type": "urn:behrouzan:problem:not-found",
"title": "Resource not found",
"status": 404,
"detail": "Product was not found.",
"errors": [
{
"code": "Product.NotFound",
"message": "Product was not found.",
"type": "NotFound",
"propertyPath": null,
"metadata": {}
}
],
"traceId": "..."
}
Validation errors are mapped to HTTP 400.
Default HTTP Mappings
The default mappings are:
| Error Type | HTTP Status |
|---|---|
Failure |
500 |
Validation |
400 |
Unauthorized |
401 |
Forbidden |
403 |
NotFound |
404 |
Conflict |
409 |
RateLimit |
429 |
Unavailable |
503 |
Timeout |
504 |
Custom Status Mapping
Mappings can be overridden during registration:
builder.Services.AddBehrouzanResultHttp(options =>
{
options.MapStatusCode(
ErrorType.Failure,
StatusCodes.Status422UnprocessableEntity);
});
Custom Problem Type Base
The default problem type base is:
urn:behrouzan:problem
It can be customized:
builder.Services.AddBehrouzanResultHttp(options =>
{
options.ProblemTypeBase =
"https://api.example.com/problems";
});
A not-found error could then produce a type such as:
https://api.example.com/problems/not-found
Non-Generic Results
Non-generic results can also be converted:
Result result = DeleteProduct(id);
return result.ToHttpResult();
A successful non-generic result returns HTTP 204 No Content.
Failures are converted to the corresponding Problem Details response.
Structured Errors
The original application errors are included in the HTTP response.
This preserves information such as:
- Error code
- Message
- Error type
- Property path
- Metadata
Clients can therefore process errors programmatically instead of depending only on human-readable messages.
Trace Identifiers
Problem responses include the current request trace identifier:
{
"traceId": "..."
}
This can be used to correlate client-side errors with server logs and diagnostics.
Dependency
This package depends on:
Behrouzan.Results
The core package remains independent from ASP.NET Core and HTTP concerns.
License
Licensed under the MIT License.
| 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. net10.0 was computed. 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. |
-
net8.0
- Behrouzan.Results (>= 0.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.