Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 2.45 KB

File metadata and controls

76 lines (56 loc) · 2.45 KB

Auto Endpoint Registration for .NET Minimal API

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.

GithubActions License Coverage Status NuGet Version

Installation

NuGet Package Manager Console:

Install-Package AutoRegisterEndpoints

Or via the .NET CLI:

dotnet add package AutoRegisterEndpoints

Configuration

follow these steps:

  1. In your Program.cs file, add the following extension method MapEndpoints to WebApplication:

    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    
    app.MapEndpoints(); // <------
    
    app.Run();
  2. Ensure that your endpoint classes implement the IEndpoint interface 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 IEndpointRouteBuilder to 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.