This package provides automatic endpoint registration for .NET Minimal API, eliminating the need to manually map endpoints in the Program.cs file. By using this library, you can streamline the process of setting up your API endpoints, making your code cleaner and more maintainable.
NuGet Package Manager Console:
Install-Package AutoRegisterEndpointsOr via the .NET CLI:
dotnet add package AutoRegisterEndpointsfollow these steps:
In your
Program.csfile, add the following extension methodMapEndpointstoWebApplication:varbuilder=WebApplication.CreateBuilder(args);varapp=builder.Build();app.MapEndpoints();// <------app.Run();
Ensure that your endpoint classes implement the
IEndpointinterface provided by the package. For example:publicclassWeatherForecastEndpoint:IEndpoint{publicvoidMap(IEndpointRouteBuilderendpointRouteBuilder){endpointRouteBuilder.MapGet("/weatherforecast",()=>{// Your endpoint logic here});}}
You could also use
IEndpointRouteBuilderto map a group of endpoints:publicclassWeatherForecastEndpoint:IEndpoint{publicvoidMap(IEndpointRouteBuilderendpointRouteBuilder){vargroup=endpointRouteBuilder.MapGroup("v1/weatherforecast");group.MapGet("/",()=>{// Your endpoint logic here});group.MapPost("/",()=>{// Your endpoint logic here});}}
By following these steps, your endpoints will be automatically registered without the need to manually map them in the Program.cs file.