Minimal APIs are not new in .NET. They have been there since .NET 6 as far as I know. So, I thought this may be right time to explore them now.
In this short article, let’s try to create a simple project using Visual Studio and try to understand the code in the default project template.
For following the steps mentioned here in this article, you should have release candidate of .NET 7 installed. I would also suggest to install visual studio 2022 preview version 17.4 . You can install it from here.
Create Minimal API Project
For this demo, we are going to use Visual Studio to create the project. Open visual studio and then select option to create new project. Then we can select option to create a new ASP .NET Core API project.
After entering the project and solution names, there is a screen in wizard for entering additional information. Below is the list of information that needs to be entered:
.NET 7in the target framework dropdown.
- There is option to select
- Then there is one checkbox to configure for
- And another checkbox for enabling docker. There is also option to select OS for docker container.
- Another option is for selecting either controllers or minimal APIs. Uncheck this option as we need to use minimal API template.
- Then there are also options to enable swagger page in the project.
- Then, select the option to do not use top level statements in the project.
Once these inputs are entered, click on Create button.
Project has three files
- Program.cs which contains Main method.
- appsettings.json, the configuration file
- WeatherForecast.cs, a POCO class
Explore the Code
Now, let’s open
Program.cs file. It contains the Main method. The code snippet given below shows the code from that method.
The code from the below snippet can be divided into two logical parts
- Part 1 – which contains code to configure services and request pipeline of the web application. This part is similar to the web API project template with controllers.
- Part 2 – which contains code to configure API endpoints.
MapGet method here takes a string and a delegate as parameter. The first parameter, which is a string, is path of the API (
/weatherforecast) and second parameter is the handler which is supposed to handle the request sent to the API URL.
As the name suggests
MapGet method can be used to handle only
Modifying the code
Let’s add a simple new endpoint, as shown below. These lines should be added after the weather forecast endpoint. As you may have guessed it already, this code would create a HTTPGET endpoint which has path “
/hello-world” and the endpoint would return a string.
Running the code
Now, let’s run the code. And we can see a swagger page. Then we can see two endpoints and we can select any endpoint and we can try it out.
As you can see, the endpoints are hardcoded here in the minimal API code. While on the other hand, the API project with controllers has different way of mapping the routes. The routing is mostly done via attributes and there are some tokens like area and controller name and action name, which can be used in routes.
If you want to use minimal APIs project, and if you have small project you can have a single file which would contain all API endpoints. It’s my guess that if the project grows beyond a certain level, then we may end up having separate files for every endpoint, and the structure may become somewhat similar to the code containing controllers.
Have you already used minimal APIs ? Do you share the same opinion ? Let me know your thoughts.