Alvecta.Foundation.Presentation.WebApi.AspNetCore 10.4.103

There is a newer version of this package available.
See the version list below for details.
dotnet add package Alvecta.Foundation.Presentation.WebApi.AspNetCore --version 10.4.103
                    
NuGet\Install-Package Alvecta.Foundation.Presentation.WebApi.AspNetCore -Version 10.4.103
                    
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="Alvecta.Foundation.Presentation.WebApi.AspNetCore" Version="10.4.103" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Alvecta.Foundation.Presentation.WebApi.AspNetCore" Version="10.4.103" />
                    
Directory.Packages.props
<PackageReference Include="Alvecta.Foundation.Presentation.WebApi.AspNetCore" />
                    
Project file
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 Alvecta.Foundation.Presentation.WebApi.AspNetCore --version 10.4.103
                    
#r "nuget: Alvecta.Foundation.Presentation.WebApi.AspNetCore, 10.4.103"
                    
#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.
#:package Alvecta.Foundation.Presentation.WebApi.AspNetCore@10.4.103
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Alvecta.Foundation.Presentation.WebApi.AspNetCore&version=10.4.103
                    
Install as a Cake Addin
#tool nuget:?package=Alvecta.Foundation.Presentation.WebApi.AspNetCore&version=10.4.103
                    
Install as a Cake Tool

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 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 I and the trailing UseCase are removed: IGetOrderUseCase maps to GetOrder, relative to the route group. TUseCase must 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), its Validate() runs before the use case, and a request with errors is answered by the mapping itself: a 400 problem (RFC 9457, Results.Problem) whose detail holds every error on a line of its own, each as path: message. Such a request never reaches the use case, so onError has no part in it. Nothing is thrown, so the host needs no exception handler for it. The check is an endpoint filter the mapping adds, so it runs before any filter the host chains on.
  • 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. Done is answered as a plain use case without a response is: 200 with no body.
  • The error is the endpoint's to answer. onError is a Func<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 with Results.Problem(detail, statusCode). There is no default for onError: 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 with Produces<TResponse>(): an OpenAPI document shows the same 200 for both forms. What onError answers with is not described. The host chains ProducesProblem(statusCode) for each status it uses.
  • A default result fails the request. A use case that returns default instead of a response, Done or an error holds neither side, and the endpoint throws a SwitchExpressionException instead 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>()

Version 10.4 answers a request that breaks a rule of its own itself, with the 400 problem described above. Until 10.3 the mapping threw the errors as a UserException and left the answer to an exception handler of the host, and the package depended on Alvecta.Utils.UserExceptions. A host that answered that exception with problem details sees the same status and the same detail, save that the errors are now separated by \n on every machine, where the exception's message had the environment's line end. A project that used the user exceptions only because this package brought them now references Alvecta.Utils.UserExceptions itself.

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)
            };
    }
}

The mapping throws nothing of its own: an error is answered by onError, and a request that breaks a rule of its own by the mapping. Both are written with Results.Problem, so they share one body, and whatever the host registers for problem details (AddProblemDetails, with its CustomizeProblemDetails) applies to both. An exception that does reach the host is a failure of the use case, which the host answers as it answers any other.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.4.104 42 12/2/2026
10.4.103 50 12/2/2026
10.4.102 53 12/1/2026
10.3.101 88 11/30/2026
10.2.100 84 11/30/2026
10.2.99 82 11/30/2026
10.1.98 76 11/30/2026
10.0.97 97 11/29/2026
9.6.96 99 11/25/2026
9.6.95 92 11/25/2026
9.6.94 98 11/24/2026
9.6.93 100 11/24/2026
9.5.92 103 11/24/2026
9.4.91 102 11/20/2026
9.3.90 101 11/13/2026
9.2.89 97 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
Loading failed