Maple.Result.Extensions.AspNetCore
0.15.0
dotnet add package Maple.Result.Extensions.AspNetCore --version 0.15.0
NuGet\Install-Package Maple.Result.Extensions.AspNetCore -Version 0.15.0
<PackageReference Include="Maple.Result.Extensions.AspNetCore" Version="0.15.0" />
<PackageVersion Include="Maple.Result.Extensions.AspNetCore" Version="0.15.0" />
<PackageReference Include="Maple.Result.Extensions.AspNetCore" />
paket add Maple.Result.Extensions.AspNetCore --version 0.15.0
#r "nuget: Maple.Result.Extensions.AspNetCore, 0.15.0"
#:package Maple.Result.Extensions.AspNetCore@0.15.0
#addin nuget:?package=Maple.Result.Extensions.AspNetCore&version=0.15.0
#tool nuget:?package=Maple.Result.Extensions.AspNetCore&version=0.15.0
Maple.Result.Extensions.AspNetCore
Maps the Maple.Result to the HTTP response (ASP.NET Core MVC version)
This package maps to the MVC ActionResult. For Minimal APIs use
Maple.Result.Extensions.MinimalApi.
Project status: EARLY STAGE
Check also my libraries:
- Maple.Result,
- Maple.Result.Extensions.Functions.Worker,
- Maple.Result.Extensions.Functions.Worker.AspNetCore,
- Maple.Result.Extensions.MinimalApi,
- Maple.Result.Extensions.HttpClient.
Give it a star ⭐
Do you like it? Show your support by giving this project a star!
Status
✅ Basic mapping without configuration.
✅ Integration tests.
✅ Configuration passed as a parameter to the extension method.
✅ Support for global configuration.
✅ Support for multiple successful codes.
✅ Support for Created (201) with Location header.
🔲 Documentation.
Usage
using Maple.Result;
using Maple.Result.Extensions.AspNetCore;
// Success -> 204 No Content, error -> RFC 9457 problem details
[HttpDelete("{id}")]
public ActionResult Delete(int id)
=> _orderService.Delete(id).ToActionResult(this);
// Success -> 200 OK with the value (204 No Content when the value is null)
[HttpGet("{id}")]
public ActionResult Get(int id)
=> _orderService.Get(id).ToActionResult(this);
// Success -> the given status code
[HttpPost]
public ActionResult Create(Order order)
=> _orderService.Create(order).ToActionResult(this, HttpStatusCode.Created);
// Success -> mapped explicitly, here to 201 Created with the Location header
[HttpPost("{id}/copy")]
public ActionResult Copy(int id)
=> _orderService.Copy(id).ToActionResult(this,
(order, controller) => controller.Created($"/orders/{order.Id}", order));
// Errors -> mapped explicitly; the default mapping is used when the mapping returns null
[HttpDelete("{id}/lines")]
public ActionResult DeleteLines(int id)
=> _orderService.DeleteLines(id).ToActionResult(this,
customErrorMapping: (error, controller) => error.Category == ErrorCategory.Conflict
? controller.StatusCode(StatusCodes.Status410Gone)
: null);
A mapping passed without a parameter name always maps the successful result, so an error mapping has to be passed as
customErrorMapping: unless a success mapping precedes it.
The same error mapping can be registered once for the whole application, in which case it is used whenever no mapping is passed to the extension method:
builder.Services.ConfigureResultMapping(options => options.ErrorMappings.Add(
(error, controller) => error.Category == ErrorCategory.Conflict
? controller.StatusCode(StatusCodes.Status410Gone)
: null));
Azure Functions
This package needs a controller, so it cannot be used in Azure Functions. Use instead:
- Maple.Result.Extensions.Functions.Worker.AspNetCore
with ASP.NET Core integration (
HttpRequestandIActionResult), - Maple.Result.Extensions.Functions.Worker
without it (
HttpRequestDataandHttpResponseData).
Example Error Response
Example
{
"type": "https://example.com/probs/out-of-credit",
"title": "You do not have enough credit.",
"status": 400,
"detail": "Your current balance is 30, but that costs 50.",
"instance": "/accounts/12345/msgs/abc",
"errors": [
{
"pointer": "#/age",
"detail": "must be a positive integer",
"detailTemplated": {
"templateId":"user.details.age.mustBePositive"
}
},
{
"pointer": "#/profile/colour",
"detail": "must be ‘green’, ‘red’ or ‘blue’",
"detailTemplated": {
"templateId": "user.profile.colour",
"params": {
"validValueIds": [
"user.profile.colour.green",
"user.profile.colour.red",
"user.profile.colour.blue"
]
}
}
}
],
"detailTemplated": {
"templateId": "user.account.balance.tooLow",
"params": {
"errorCode": "UAB17",
"accounts": [
{
"title": "Main (***9456)",
"url": "/accounts/12345"
},
{
"title": "Main (***3357)",
"url": "/accounts/67890"
}
],
"currentBalance": 30,
"requiredBalance": 50
}
}
}
See also
- Problem Details for HTTP APIs - RFC 7807 is dead, long live RFC 9457
- tag URI scheme
- RFC 1738: Uniform Resource Locators (URL)
- RFC 3986: Uniform Resource Identifier (URI): Generic Syntax
- RFC 4151: The 'tag' URI Scheme
- RFC 6901: JavaScript Object Notation (JSON) Pointer
- RFC 9457: Problem Details for HTTP APIs
Contribution
Please contact author: engineer(at sign)blumail(dot)me
| 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
- Maple.Result (>= 2.8.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.