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:var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapEndpoints(); // <------ app.Run();
-
Ensure that your endpoint classes implement the
IEndpointinterface provided by the package. For example:public class WeatherForecastEndpoint : IEndpoint { public void Map(IEndpointRouteBuilder endpointRouteBuilder) { endpointRouteBuilder.MapGet("/weatherforecast", () => { // Your endpoint logic here }); } }
You could also use
IEndpointRouteBuilderto map a group of endpoints:public class WeatherForecastEndpoint : IEndpoint { public void Map(IEndpointRouteBuilder endpointRouteBuilder) { var group = 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.