Well, you can have partial Program class:
partial class Program
{
static void MapProductEndpoints(WebApplication app)
{
app.MapGet("/products/{id}", (int id) => Results.Ok());
}
}
var app = builder.Build();
MapProductEndpoints(app);
or you can have static class or an extension method:
public static class ProductEndpoints
{
public static void Map(WebApplication app)
{
app.MapGet("/products/{id}", (int id) => Results.Ok());
}
}
var app = builder.Build();
ProductEndpoints.Map(app);
public static class WebApplicationProductEndpointsExtensions
{
public static void MapProductEndpoints(this WebApplication app)
{
app.MapGet("/products/{id}", (int id) => Results.Ok());
}
}
var app = builder.Build();
app.MapProductEndpoints();
or you can wrap it in an interface and do assembly scanning:
public interface IEndpoints
{
static abstract void Map(WebApplication app);
}
public class ProductEndpoints : IEndpoints
{
public static void Map(WebApplication app)
{
app.MapGet("/products/{id}", (int id) => Results.Ok());
}
}
var app = builder.Build();
var assembly = Assembly.GetExecutingAssembly();
var endpointsCollection = assembly
.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(IEndpoints)) && !t.IsInterface);
foreach (var endpoints in endpointsCollection)
{
var map = endpoints.GetMethod(nameof(IEndpoints.Map));
map.Invoke(null, new[] { app });
}
You can also try to do endpoint per file though that's trickier to enforce.