MinimalHelpers.Routing.Analyzers 1.0.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package MinimalHelpers.Routing.Analyzers --version 1.0.9                
NuGet\Install-Package MinimalHelpers.Routing.Analyzers -Version 1.0.9                
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="MinimalHelpers.Routing.Analyzers" Version="1.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MinimalHelpers.Routing.Analyzers --version 1.0.9                
#r "nuget: MinimalHelpers.Routing.Analyzers, 1.0.9"                
#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.
// Install MinimalHelpers.Routing.Analyzers as a Cake Addin
#addin nuget:?package=MinimalHelpers.Routing.Analyzers&version=1.0.9

// Install MinimalHelpers.Routing.Analyzers as a Cake Tool
#tool nuget:?package=MinimalHelpers.Routing.Analyzers&version=1.0.9                

Minimal APIs Helpers

Lint Code Base License: MIT

A collection of helpers libraries for Minimal API projects.

MinimalHelpers.Routing

Nuget Nuget

A library that provides Routing helpers for Minimal API projects for automatic endpoints registration using Reflection.

Installation

The library is available on NuGet. Just search for MinimalHelpers.Routing in the Package Manager GUI or run the following command in the .NET CLI:

dotnet add package MinimalHelpers.Routing

Usage

Create a class to hold your route handlers registration and make it implementing the IEndpointRouteHandlerBuilder interface:

.NET 6.0

public class PeopleHandler : MinimalHelpers.Routing.IEndpointRouteHandlerBuilder
{
    public void MapEndpoints(IEndpointRouteBuilder endpoints)
    {
        endpoints.MapGet("/api/people", GetList);
        endpoints.MapGet("/api/people/{id:guid}", Get);
        endpoints.MapPost("/api/people", Insert);
        endpoints.MapPut("/api/people/{id:guid}", Update);
        endpoints.MapDelete("/api/people/{id:guid}", Delete);
    }

    // ...
}

.NET 7.0 or higher

public class PeopleHandler : MinimalHelpers.Routing.IEndpointRouteHandlerBuilder
{
    public static void MapEndpoints(IEndpointRouteBuilder endpoints)
    {
        endpoints.MapGet("/api/people", GetList);
        endpoints.MapGet("/api/people/{id:guid}", Get);
        endpoints.MapPost("/api/people", Insert);
        endpoints.MapPut("/api/people/{id:guid}", Update);
        endpoints.MapDelete("/api/people/{id:guid}", Delete);
    }

    // ...
}

Note Starting from .NET 7.0, the IEndpointRouteHandlerBuilder interface exposes the MapEndpoints method as static abstract, so it can be called without creating an instance of the handler.

Call the MapEndpoints() extension method on the WebApplication object inside Program.cs before the Run() method invocation:

// using MinimalHelpers.Routing;
app.MapEndpoints();

app.Run();

By default, MapEndpoints() will scan the calling Assembly to search for classes that implement the IEndpointRouteHandlerBuilder interface. If your route handlers are defined in another Assembly, you have two alternatives:

  • Use the MapEndpoints() overload that takes the Assembly to scan as argument
  • Use the MapEndpointsFromAssemblyContaining<T>() extension method and specify a type that is contained in the Assembly you want to scan

You can also explicitly decide what types (among the ones that implement the IRouteEndpointHandlerBuilder interface) you want to actually map, passing a predicate to the MapEndpoints method:

app.MapEndpoints(type =>
{
    if (type.Name.StartsWith("Products"))
    {
        return false;
    }

    return true;
});

Note These methods rely on Reflection to scan the Assembly and find the classes that implement the IEndpointRouteHandlerBuilder interface. This can have a performance impact, especially in large projects. If you have performance issues, consider using the explicit registration method. Moreover, this solution is incompatibile with Native AOT.

If you're working with .NET 7.0 or higher, the reccommended approach is to use the MinimalHelpers.Routing.Analyzers package, that provides a Source Generator for endpoints registration, as described later.

MinimalHelpers.Routing.Analyzers (.NET 7.0 or higher)

Nuget Nuget

A library that provides a Source Generator for automatic endpoints registration in Minimal API projects.

Installation

The library is available on NuGet. Just search for MinimalHelpers.Routing in the Package Manager GUI or run the following command in the .NET CLI:

dotnet add package MinimalHelpers.Routing.Analyzers

Usage

Create a class to hold your route handlers registration and make it implementing the IEndpointRouteHandlerBuilder interface:

public class PeopleHandler : IEndpointRouteHandlerBuilder
{
    public static void MapEndpoints(IEndpointRouteBuilder endpoints)
    {
        endpoints.MapGet("/api/people", GetList);
        endpoints.MapGet("/api/people/{id:guid}", Get);
        endpoints.MapPost("/api/people", Insert);
        endpoints.MapPut("/api/people/{id:guid}", Update);
        endpoints.MapDelete("/api/people/{id:guid}", Delete);
    }

    // ...
}

Note You only need to use the MinimalHelpers.Routing.Analyzers package. With this Source Generator, the IEndpointRouteHandlerBuilder interface is auto-generated.

Call the MapEndpoints() extension method on the WebApplication object inside Program.cs before the Run() method invocation:

app.MapEndpoints();

app.Run();

Note The MapEndpoints method is generated by the Source Generator.

MinimalHelpers.OpenApi

Nuget Nuget

A library that provides OpenApi helpers for Minimal API projects.

Installation

The library is available on NuGet. Just search for MinimalHelpers.OpenApi in the Package Manager GUI or run the following command in the .NET CLI:

dotnet add package MinimalHelpers.OpenApi

Usage

Extension methods for OpenApi

This library provides some extensions methods that simplify the OpenAPI configuration in Minimal API projects. For example, it is possible to customize the description of a response using its status code:

endpoints.MapPost("login", LoginAsync)
    .AllowAnonymous()
    .WithValidation<LoginRequest>()
    .Produces<LoginResponse>(StatusCodes.Status200OK)
    .Produces<LoginResponse>(StatusCodes.Status206PartialContent)
    .Produces(StatusCodes.Status403Forbidden)
    .ProducesValidationProblem()
    .WithOpenApi(operation =>
    {
        operation.Summary = "Performs the login of a user";

        operation.Response(StatusCodes.Status200OK).Description = "Login successful";
        operation.Response(StatusCodes.Status206PartialContent).Description = "The user is logged in, but the password has expired and must be changed";
        operation.Response(StatusCodes.Status400BadRequest).Description = "Incorrect username and/or password";
        operation.Response(StatusCodes.Status403Forbidden).Description = "The user was blocked due to too many failed logins";

        return operation;
    });

Contribute

The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.0.13 473 10/15/2024
1.0.12 194 10/4/2024
1.0.10 378 9/19/2024
1.0.9 205 9/2/2024
1.0.8 606 6/25/2024
1.0.7 1,105 3/26/2024
1.0.6 136 3/12/2024