NashTech Insights

Minimal API – how to organize the minimal API structure effectively

Huỳnh Minh Tú
Huỳnh Minh Tú
Table of Contents
woman writing on whiteboard

Create a Minimal API project

Before we analyze on proper ways to organize APIs, let’s create a simple Minimal API project.

From the visual studio, create an ASP.NET Core Web app.

At the step of filling in additional information, uncheck the Use Controllers ( uncheck to use minimal APIs )

Click Create then we have a very basic minimal API setup.

1. #Region blocks

The simplest way to organize multiple Minimal APIs is to use #region code blocks. You can have various blocks in the Program file that specify the grouped functions.

Pros: The easiest way to setup. This method appropriates for a very simple demo or tutorial projects.

Cons: When the project scales with multiple areas and numerous functions. This cannot be applied since it is hard to maintain.

2. Local functions

Another easy way to organize minimal APIs is to put them inside local functions as handlers. And then call those local functions from the main code. Local functions must be placed before any type of definitions in the Program.cs file.

Pros: Now we have the business logic implementation separate out of the API mapping.

Cons: Still not be able to apply to a scaled project. There will be many mappings and business logic implementation in the Program file.

3. Static extension methods inside a static class (Recommended)

The most recommended way to organize your Minimal APIs structure is to use static extension methods inside a static class. The below image show how to implement that approach.

Pros: API mappings alongside with their business logic are grouped into areas/categories which adopt extensibility, modifiability, and scalability.

Cons: Maintain the list of static classes alongside their business logic.

4. Static method inside a static class

Yet another alternative way in static class comparing to the above static extension method is to use just a static method

Pros: API mappings alongside with their business logic is grouped into areas/categories which adopt extensibility, modifiability, and scalability.

Cons: Code is not clean compared to the extension method above.

5. Separated class with constructor injection

The last way is to use an object (not static) with its inside implementation. Let’s consider we could have a DI service/repository. Then the calculator object should not be created explicitly as new Calculator() in the program. Instead we have to inject the Calculator object if we would like to use.

Pros: Utilize OOP for class and object specifications, along with the ability to inject additional dependencies (DI) if desired.

Cons: Quite complex to setup


There are several ways on setting up your own Minimal APIs projects. Based on this article, we can decide which is the most appropriate approach. It depends upon your type of project, the purpose, and the level of scaling.

Huỳnh Minh Tú

Huỳnh Minh Tú

Senior Software Engineer

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: