Alvecta.Foundation.Presentation.WebApi.AspNetCore
10.0.97
dotnet add package Alvecta.Foundation.Presentation.WebApi.AspNetCore --version 10.0.97
NuGet\Install-Package Alvecta.Foundation.Presentation.WebApi.AspNetCore -Version 10.0.97
<PackageReference Include="Alvecta.Foundation.Presentation.WebApi.AspNetCore" Version="10.0.97" />
<PackageVersion Include="Alvecta.Foundation.Presentation.WebApi.AspNetCore" Version="10.0.97" />
<PackageReference Include="Alvecta.Foundation.Presentation.WebApi.AspNetCore" />
paket add Alvecta.Foundation.Presentation.WebApi.AspNetCore --version 10.0.97
#r "nuget: Alvecta.Foundation.Presentation.WebApi.AspNetCore, 10.0.97"
#:package Alvecta.Foundation.Presentation.WebApi.AspNetCore@10.0.97
#addin nuget:?package=Alvecta.Foundation.Presentation.WebApi.AspNetCore&version=10.0.97
#tool nuget:?package=Alvecta.Foundation.Presentation.WebApi.AspNetCore&version=10.0.97
Alvecta.Foundation.Presentation.WebApi.AspNetCore
Maps Alvecta use cases (Alvecta.Foundation.Application.Contracts) as ASP.NET Core minimal-API
endpoints, one line per use case, and adds a /version endpoint. Formerly
Alvecta.Foundation.Api.AspNetCore.
It belongs to the Presentation layer: an inbound (driving) adapter that calls use-case
interfaces and never sees their implementations. Dependencies: the Microsoft.AspNetCore.App
shared framework, Alvecta.Utils, Alvecta.Utils.UserExceptions and
Alvecta.Foundation.Application.Contracts.
What it contains
Extension methods on IEndpointRouteBuilder. Each use-case interface has a method named after it,
and each form with an error takes onError, which says what the error becomes:
| Method | Use case | Endpoint |
|---|---|---|
MapUseCase<TUseCase, TRequest, TResponse>() |
IUseCase<TRequest, TResponse> |
POST, JSON body, JSON response |
MapUseCase<TUseCase, TRequest, TResponse, TError>(onError) |
IUseCase<TRequest, TResponse, TError> |
POST, JSON body, JSON response or the error's answer |
MapUseCaseWithoutRequest<TUseCase, TResponse>() |
IUseCaseWithoutRequest<TResponse> |
POST, no body, JSON response |
MapUseCaseWithoutRequest<TUseCase, TResponse, TError>(onError) |
IUseCaseWithoutRequest<TResponse, TError> |
POST, no body, JSON response or the error's answer |
MapUseCaseWithoutResponse<TUseCase, TRequest>() |
IUseCaseWithoutResponse<TRequest> |
POST, JSON body, no response body |
MapUseCaseWithoutResponse<TUseCase, TRequest, TError>(onError) |
IUseCaseWithoutResponse<TRequest, TError> |
POST, JSON body, no response body or the error's answer |
MapUseCaseWithoutRequestAndResponse<TUseCase>() |
IUseCaseWithoutRequestAndResponse |
POST, no body, no response body |
MapUseCaseWithoutRequestAndResponse<TUseCase, TError>(onError) |
IUseCaseWithoutRequestAndResponse<TError> |
POST, no body, no response body or the error's answer |
MapVersionEndpoint() |
— | GET /version, anonymous, the entry assembly's informational version as text |
- Routes come from the interface name. The leading
Iand the trailingUseCaseare removed:IGetOrderUseCasemaps toGetOrder, relative to the route group.TUseCasemust be an interface named that way, otherwise mapping throws at startup. - Every use case is a
POST. The use case is resolved from DI per request. - Requests are validated first, in every form that takes a request. When a request implements
IValidatable(Alvecta.Utils.Validation), itsValidate()runs before the use case, and any errors are thrown as aUserException. Such a request never reaches the use case, soonErrorhas no part in it. - A result never goes out as it is. A form with an error writes the response exactly as the
plain form writes it: the same status, content type and body.
Doneis answered as a plain use case without a response is: 200 with no body. - The error is the endpoint's to answer.
onErroris aFunc<TError, IResult>of the presentation layer. An error carries facts and no words, so the words and the status are chosen where the transport is known, for example withResults.Problem(detail, statusCode). There is no default foronError: a default would need every error to describe itself. - The response is described as the plain form's is. The handler of a form with an error
returns an
IResult, from which ASP.NET Core cannot tell the response, so a form with a response declares it withProduces<TResponse>(): an OpenAPI document shows the same 200 for both forms. WhatonErroranswers with is not described. The host chainsProducesProblem(statusCode)for each status it uses. - A
defaultresult fails the request. A use case that returnsdefaultinstead of a response,Doneor an error holds neither side, and the endpoint throws aSwitchExpressionExceptioninstead of passing it for a success. - Each method returns the
RouteHandlerBuilder, so authorization, filters and metadata chain on as usual.
Version 10.0 renamed two methods with the interfaces they map:
| Until 9.x | From 10.0 |
|---|---|
MapUseCase<TUseCase, TRequest>() |
MapUseCaseWithoutResponse<TUseCase, TRequest>() |
MapUseCase<TUseCase>() |
MapUseCaseWithoutRequestAndResponse<TUseCase>() |
Usage
dotnet add package Alvecta.Foundation.Presentation.WebApi.AspNetCore
using Alvecta.Foundation.Application.Contracts.Database;
using Alvecta.Foundation.Presentation.WebApi.AspNetCore;
var app = builder.Build();
app.MapVersionEndpoint();
var orders = app.MapGroup("/api/orders").RequireAuthorization();
// POST /api/orders/GetOrder
orders.MapUseCase<IGetOrderUseCase, IGetOrderUseCase.Request, IGetOrderUseCase.Response>();
// POST /api/orders/CancelOrder
orders.MapUseCaseWithoutResponse<ICancelOrderUseCase, ICancelOrderUseCase.Request>();
// POST /api/orders/PlaceOrder
orders.MapUseCase<
IPlaceOrderUseCase,
IPlaceOrderUseCase.Request,
IPlaceOrderUseCase.Response,
IPlaceOrderUseCase.Error>(
OrderProblems.ToProblem);
var database = app.MapGroup("/api/database").RequireAuthorization("ApiKeyPolicy");
// POST /api/database/MigrateDatabase
database.MapUseCaseWithoutRequestAndResponse<IMigrateDatabaseUseCase>();
app.Run();
OrderProblems.ToProblem is what the host passes as onError: a method of the presentation layer
that gives each error its words and its status.
public static class OrderProblems
{
public static IResult ToProblem(IPlaceOrderUseCase.Error error)
{
return
error switch
{
IPlaceOrderUseCase.Error.OutOfStock outOfStock
=> Results.Problem($"The product {outOfStock.ProductId} is out of stock.", statusCode: StatusCodes.Status409Conflict),
IPlaceOrderUseCase.Error.Discontinued discontinued
=> Results.Problem($"The product {discontinued.ProductId} is no longer sold.", statusCode: StatusCodes.Status410Gone)
};
}
}
Turning a UserException into an HTTP response is up to the host, for example with the
UserExceptionHandler from Alvecta.Utils.AspNetCore (AddUserExceptionHandler() and
UseExceptionHandler()). It writes problem details as Results.Problem does, so a user error
that is thrown and an error that onError answers share one body.
| 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
- Alvecta.Foundation.Application.Contracts (>= 10.0.97)
- Alvecta.Utils (>= 9.0.91)
- Alvecta.Utils.UserExceptions (>= 9.0.91)
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 |
|---|---|---|
| 10.0.97 | 0 | 11/29/2026 |
| 9.6.96 | 58 | 11/25/2026 |
| 9.6.95 | 53 | 11/25/2026 |
| 9.6.94 | 59 | 11/24/2026 |
| 9.6.93 | 61 | 11/24/2026 |
| 9.5.92 | 64 | 11/24/2026 |
| 9.4.91 | 100 | 11/20/2026 |
| 9.3.90 | 101 | 11/13/2026 |
| 9.2.89 | 96 | 11/12/2026 |
| 9.1.88 | 95 | 11/11/2026 |
| 9.0.87 | 104 | 11/4/2026 |
| 9.0.86 | 95 | 11/4/2026 |
| 9.0.85 | 92 | 11/4/2026 |
| 9.0.84 | 98 | 11/4/2026 |
| 9.0.83 | 108 | 10/29/2026 |
| 9.0.82 | 110 | 10/29/2026 |
| 8.1.81 | 108 | 10/29/2026 |
| 8.0.80 | 102 | 10/28/2026 |
| 8.0.79 | 98 | 10/28/2026 |
| 8.0.78 | 114 | 10/28/2026 |