.Net 6.0 minimal API is a new approach to building APIs without all complex structures of MVC. Minimal API contains just the essential components needed to build HTTP APIs, which are just the csproj and a program.cs file. They do not replace the MVC framework but just an alternative way of building REST APIs which has made it extremely simple with minimal dependencies.
Benefits Of Minimal API
- Reduce the ceremony to create a new API
- API without controllers
- Powered by .Net 6 and C# 10
- Faster compared to MVC as the bootstrapping that is required to build and compile the application is simplified
Let’s learn by getting our hands dirty
Prerequisites:
- Download .NET 6 SDK and ASP.NET Core Runtime
- Visual Studio 2022
Create a new project using Visual Studio 2022 and select the “ASP.NET Core Empty” project template.
Add alt text
Proceed further and select “.Net 6.0 (Long-term support) and hit create
Add alt text
This creates the API project with Program.cs class which has everything we need to expose the API,
Add alt text
app.MapGet("/", () => "Hello World!");
This one line of code is the API which lifts the process of adding controller and action to expose the API. In the minimal API, we can specify what HTTP action needs to be done using app.Map<ACTION> method. In our example, it instructs to create a GET API with “app.MapGet” method. The first parameter is the path to the API and the second parameter is the actual function to be executed. In our example the base path is “/” and action is a function that has no input parameters and returns the text “Hello World!”. When we run this in the browser it looks like this,
Add alt text
Now let’s play around a bit by adding a new API to get all customer information. I have added a new model project named “MinimalApi.Model” and a Customer model class named “Customer.cs”,
Add alt text
and exposed a GET API with path “/customers”,
Add alt text
Add alt text
Let’s refactor further to move the API to a new class,
Add alt text
I have moved the whole API to a new class named “CustomerApi” and extended the type “WebApplication” so that I can register it in the Program.cs as below,
Add alt text
Add alt text
Dependency Injection
Time to add a few more APIs but before that let me refactor it further to move the data to the data access layer. Still, data would be hardcoded as I am not connecting to a database as the whole purpose is to show how to implement dependency injection in minimal API.
With Minimal APIs, we can still benefit from dependency injection but instead of using constructor injection, the dependencies are passed as parameters in the handler delegate.
Let’s do that. I have added a data access project which has an interface IRepository and a class CustomerRepository that implements IRepositiry,
Add alt text
And move the data to CustomerRepository class,
Add alt text
and update CustomerApi.cs to inject IRepository to the method “GetAllCustomers”,
Add alt text
The dependency should be resolved in Program.cs as below,
Add alt text
Add alt text
More APIs
Now time to add more APIs to the refactored code. Let’s add another GET API to get a customer by Id.
Added method to filter customer by id in the repository class,
Add alt text
and added a new route that accepts “{id}” parameter in the CusomerApi.cs,
Add alt text
Add alt text
POST API
On POST request minimal API do the model binding automatically which is the same in MVC controller if you add [ApiController] attribute. If you are not familiar with what model binding is then model binding is the process of retrieving values from the HTTP request and converting them to .NET types. Here we mainly look at receiving JSON data in the request body. I am skipping the repository part as my main intention while introducing repository was to show dependency injection which we have covered already. With that let’s add a new API that POSTs customer data,
Add alt text
And the POST request is,
Add alt text
As you can see it is as simple as adding a path with “app.MapPost” method. Also, you can see the model bound automatically
DELETE API
Add alt text
Add alt text
PUT API
And when you want to update the record you should open a path with “app.MapPut”. Here as well model binds automatically,
Add alt text
Add alt text
Interestingly there is no “MapPatch” method but you can define any set of verbs using “MapMethods”
Conclusion
In this article, we have learned about minimal APIs in .NET 6 and explored how to define routes and handlers using Minimal APIs, use the Map(Get|Post|Put|Delete) methods. Minimal APIs offer an alternative approach for building APIs with .Net 6. The main benefit is that you have a lightweight base compared to MVC. In many cases, this can result in subsequent performance gains.