Minimal API in dotnet

Satyam Pushkar
4 min readApr 20, 2022

This article showcases how simple it is for anyone to create a minimal API using dotnet 6.

Being a dotnet developer I always envied NodeJS developers that they can create API in a single file. On the other hand as a dotnet developer, you must had needed at least need 3 files: startup.cs, programm.cs and one controller. But that’s not the case anymore!! In November 2021 dotnet introduces Minimal API and now I can create an API with just one file in dotnet.

Minimal APIs are designed by dotnet team to create HTTP APIs with minimal dependencies. These are very apt for microservices and any other apps that want to have only the minimum files, features, and dependencies in ASP.NET Core.

Recently I was trying to understand different microservices architecture and stumbled upon minimal API and now I am absolutely in love with it. If you want to create a sample microservice for test or analysis purpose, you can spin up an API quickly with just one file. And why only for test or analysis only, you can create a full fledged enterprise grade APIs/applications using Minimal APIs. And talking about performance it is faster than the controller based approach, (to know more please read this article).

Benefits of Minimal API

  • Reduced complexity
  • Easy to get started. You can run an API with just 3 lines of code.
  • Increased performance. Microsoft has done a lot of bootstrapping to make application compile, build and run faster.

Create Minimal API with Command Line

Run the following command to create a Minimal API.

dotnet new web -o MinAPIfrmCmdLine

This will create an API under the folder ‘MinAPIfrmCmdLine’. Here if you check the folder, you will find there are only 2 important files here: Program.cs and .csproj file. And if you check the content of Program.cs, you will find there are only 4 lines of code.

Once you run the app, you will find that the app is running and listening to localhost. You can see that how easy it has become now to create and run an API in dotnet.

Create Minimal API with Visual Studio

In Visual Studio 2022, select the option to create a new project. Choose the template “ASP .NET Core Web API”.

After that you will be asked to provide the Project Name, Location to save and Solution Name. Please enter it appropriately. On next screen, you will find the option to fill the following information.

You can choose based on your choice. But please make sure to uncheck the option “Use Controller” to create a Minimal API.

That’s it!! Once you click on Create button, your API(Minimal API), without controllers!! In a single file!! is ready.

The template provided by Microsoft creates an API to show weather forecast. You can see how the swagger page and console are created denoting the API is ready to serve requests.

This was a sample demo showing how to create Minimal API in dotnet. Hope you found this article useful.

For further reading on Minimal API you can check out these Microsoft docs:

  1. Create a minimal web API with ASP.NET Core
  2. Minimal APIs overview

Please do checkout other articles by me and share your feedback(s) if any. Thank you and Happy Coding :)

--

--

Satyam Pushkar

Software Engineer | Backend Specialist | System Architect | Cloud Native Apps | DOTNET, PYTHON | https://www.linkedin.com/in/satyampushkar/