Skip to content

Instantly share code, notes, and snippets.

@VincentH-Net
Created April 9, 2024 18:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VincentH-Net/a3a5c77904f250c2b391e294d595cd5d to your computer and use it in GitHub Desktop.
Save VincentH-Net/a3a5c77904f250c2b391e294d595cd5d to your computer and use it in GitHub Desktop.
ASP.NET Core Minimal API's Endpoints registration helper
// Endpoints registration helper
// Enables DI for a group of endpoints to reduce init code and repeating parameters across endpoints
// Usage in e.g. Program.cs:
app.RegisterEndpoints(
typeof(CatalogEndpoints),
typeof(BasketsEndpoints)
);
// Implementation:
public interface IEndpoints
{
void Register(IEndpointRouteBuilder routeBuilder);
}
public static class WebApplicationExtensions
{
public static void RegisterEndpoints(this WebApplication app, params Type[] endpointsTypes)
{
foreach (var endpointsType in endpointsTypes)
{
((IEndpoints)ActivatorUtilities.CreateInstance(app.Services, endpointsType))
.Register(app);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment