Genesis.MinimalApis 7.1.0

dotnet add package Genesis.MinimalApis --version 7.1.0
NuGet\Install-Package Genesis.MinimalApis -Version 7.1.0
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="Genesis.MinimalApis" Version="7.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Genesis.MinimalApis --version 7.1.0
#r "nuget: Genesis.MinimalApis, 7.1.0"
#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 Genesis.MinimalApis as a Cake Addin
#addin nuget:?package=Genesis.MinimalApis&version=7.1.0

// Install Genesis.MinimalApis as a Cake Tool
#tool nuget:?package=Genesis.MinimalApis&version=7.1.0

Genesis Minimal Endpoints

.NET CodeQL nuget license

Create a minimal endpoint from any function in any file.

Genesis.MinimalApis provides support for easily exposing minimal endpoints from any file.

Important!

MapPatch and HttpPatchAttribute are only supported in dotnet 7.0 and greater! Use MapPost and HttpPostAttribute in dotnet 6.0!

Please reference MapPatch

Getting Started

Add the following package from nuget

dotnet add package Genesis.MinimalApis

Usage

Any function in pretty much any file can be exposed as a minimal endpoint via adding the IEndpoints interface to a class or struct or by adding any HttpMethodAttribute found in Microsoft.AspNetCore.Mvc.

Full DI support is provided for endpoints registered either manually or by the use of attributes. Additional examples are provided under Samples.

Endpoints can be registered under any lifecycle.

  • Transient
  • Scoped
  • Singleton

If registering a class that inherits from IEndpoints helper dependency injection functions for services are provided.

  • AddTransientEndpoints<T>(this IServiceCollection services) where T : IEndpoint
  • AddScopedEndpoints<T>(this IServiceCollection services) where T : IEndpoint
  • AddSingletonEndpoints<T>(this IServiceCollection services) where T : IEndpoint

Structs that inherit from IEndpoints may also be registered, though they require a but more setup.

  • RegisterEndpoints<T>(this IServiceCollection services, Func<ServiceProvider, object> func);

Static classes can be registered when using attributes found in Microsoft.AspNetCore.Mvc.

Manual Definition

Endpoints can be defined and used in class files by using the IEndpoints interface.

//. GreetingService.cs
internal sealed class GreetingService : IEndpoints {
    //. function to expose as an endpoint
    public string Greet(string name) =>
        $"Hello {name}!";

    //. function from interface to which will be used to register the app.
    public void RegisterEndpoints(IEndpointRouteBuilder app) {
        app.MapGet("greet/{name}", Greet)
    };
}

//. Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingletonEndpoints<GreetingService>();

var app = builder.Build();

//. Register endpoint GET /greet/{name} in GreetingService.cs
app.MapEndpoints<GreetingService>();

app.Run();

Attribute Definition

Endpoints can be exposed by using attributes found in Microsoft.AspNetCore.Mvc and Microsoft.AspNetCore.Mvc.Routing.

Supported Attributes:

//. GreetingService.cs
using Microsoft.AspNetCore.Mvc;

[Route("api")]
internal sealed class GreetingService {
    [HttpGet("greet/{name}")]
    public string Greet(string name) =>
        $"Hello {name}!";
}

//. Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<GreetingService>();

var app = builder.Build();

//. Map attributes in GreetingService to GET /api/Greet/{name}
app.MapEndpoints<GreetingService>();

app.Run();

Static Attribute Definition

Attributes can be used on static classes such that static functions can be registered as endpoint.

Only method injection will work using this method of registration as static classes cannot be instantiated.

//. GreetingFunctions.cs
using Microsoft.AspNetCore.Mvc;

[Route("api")]
internal static class GreetingFunctions {
    [HttpGet("greet/{name}")]
    public static string Greet(string name) =>
        $"Hello {name}!";
}

//. Program.cs
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

//. Map attributes in GreetingFunctions to GET /api/Greet/{name}
app.MapStaticEndpoints<GreetingService>();

app.Run();
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Genesis.MinimalApis:

Package Downloads
Genesis.LanguageExt.AspNetCore

Converts monad types from LanguageExt to AspNet Core Result types

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.1.0 172 5/30/2023
7.0.0 262 1/20/2023
6.3.1 280 12/22/2022
6.3.0 272 12/21/2022
6.2.0 357 10/5/2022
6.1.1 443 9/30/2022
6.1.0 471 9/27/2022