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
                    
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.0.97" />
                    
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.0.97" />
                    
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.0.97
                    
#r "nuget: Alvecta.Foundation.Presentation.WebApi.AspNetCore, 10.0.97"
                    
#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.0.97
                    
#: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.0.97
                    
Install as a Cake Addin
#tool nuget:?package=Alvecta.Foundation.Presentation.WebApi.AspNetCore&version=10.0.97
                    
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, 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 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 any errors are thrown as a UserException. Such a request never reaches the use case, so onError has 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. 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>()

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 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.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
Loading failed