MinimalApis.Extensions
A set of extensions and helpers that extend the functionality of ASP.NET Core Minimal APIs.
Installation
Prerelease Builds
This package is currently available in prerelease from nuget.org:
> dotnet add package MinimalApis.Extensions --prerelease
CI Builds
If you wish to use builds from this repo's main
branch you can install them from this repo's package feed.
-
Create a personal access token for your GitHub account with the
read:packages
scope with your desired expiration length: -
At the command line, navigate to your user profile directory and run the following command to add the package feed to your NuGet configuration, replacing the
<GITHUB_USER_NAME>
and<PERSONAL_ACCESS_TOKEN>
placeholders with the relevant values:~> dotnet nuget add source -n GitHub -u <GITHUB_USER_NAME> -p <PERSONAL_ACCESS_TOKEN> https://nuget.pkg.github.com/DamianEdwards/index.json
-
You should now be able to add a reference to the package specifying a version from the repository packages feed
-
See these instructions for further details about working with GitHub package feeds
Getting Started
- Install the NuGet package into your ASP.NET Core project:
> dotnet add package MinimalApis.Extensions --prerelease
- In your project's
Program.cs
, call theAddEndpointsMetadataProviderApiExplorer()
method onbuilder.Services
to enable enhanced endpoint metadata inApiExplorer
:var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsMetadataProviderApiExplorer(); // <-- Add this line builder.Services.AddSwaggerGen(); ...
- Update your Minimal APIs to use the helper binding and result types from this library, e.g.:
app.MapPost("/todos", async Task<Results<ValidationProblem, Created<Todo>>> (Validated<Todo> input, TodoDb db) => { if (!input.IsValid) return Results.Extensions.ValidationProblem(input.Errors); var todo = input.Value; db.Todos.Add(todo); await db.SaveChangesAsync(); return Results.Extensions.Created($"/todos/{todo.Id}", todo); });
What's Included?
This library provides types that help extend the core functionality of ASP.NET Core Minimal APIs in the following ways:
- Enhanced parameter binding
- Typed
IResult
objects for easier unit testing (available viaResults.Extensions
) - Automatic population of detailed endpoint descriptions in Swagger/OpenAPI via the ability for input and result types to add to endpoint metadata via
IProvideEndpointParameterMetadata
andIProvideEndpointResponseMetadata
- Union
IResult
return types viaResults<TResult1, TResultN>
that enable route handler delegates to declare all the possibleIResult
types they can return, enabling compile-time type checking and automatic population of possible responses in Swagger/OpenAPI
Sample Projects
TodoApis.Dapper
An example Todos application using ASP.NET Core Minimal APIs and the Dapper library for data storage in SQLite.
MinimalApis.Examples
Contains small examples for other types in this library.
MinimalApiPlayground
Shows many examples of using the types in this library along with other things related to ASP.NET Core Minimal APIs.