Architecting a minimal API project
Up to now, we have written route handlers directly in the Program.cs
file. This is a perfectly supported scenario: with minimal APIs, we can write all our code inside this single file. In fact, almost all the samples show this solution. However, while this is allowed, we can easily imagine how this approach can lead to unstructured and therefore unmaintainable projects. If we have fewer endpoints, it is fine – otherwise, it is better to organize our handlers in separate files.
Let’s suppose that we have the following code right in the Program.cs
file because we have to handle CRUD operations:
app.MapGet("/api/people", (PeopleService peopleService) => { }); app.MapGet("/api/people/{id:guid}", (Guid id, PeopleService peopleService) => { }); app.MapPost("...