DEV Community

Cover image for Getting started with Minimal APIs in .Net 6
Mohamad Lawand
Mohamad Lawand

Posted on

Getting started with Minimal APIs in .Net 6

Intro

This week Microsoft has released .Net 6, which is major mile stone. .Net 6 is an LTS release which means its support for the next 3 years.

Part of this major release Asp.Net Core Minimal API where released. In this video we will take a look at Minimal API discuss what they are how do they work and how we can get started with it.

You can watch the full video on Youtube

You can get the source code on GitHub using the link below
https://github.com/mohamadlawand087/v52-minimalAPi

So what are we going to cover today:

  • What is Minimal API
  • Benefits of Minimal API
  • Ingredients
  • Code time

As always you will find the source code in the description down below. Please like, share and subscribe if you like the video. It will really help the channel

What is Minimal API

It is a fresh new approach to building APIs without all the complex structure of MVC, it could seems like a prototyping tool for some but for other it could look like a stepping stone into the world of APIs.

Minimal simply means that it contains the essential components needed to build HTTP APIs. Basically with minimal API all you need is a csproj and a program.cs to get started

Minimal API uses the power of C# 9 and .NET 6.0 it combines Top Level statement and Lambda Attributes of C# and abstracting alot of the pipeline and configuration of MVC

Benefit of Minimal API

  • Reduce complexity
  • Reduce ceremony for all developers
  • Easy to get started: all you need is the Program.cs and your csproj and 3 lines of code to create your first API
  • Its powered by .Net 6 and C# 10 so all of the latest improvements and functionalities are supported out of the box
  • API without controllers
  • Performance: since the application is so simple alot og bootstrapping that is required to build and compile the application is simplified which means the application runs much faster

Ingredients

dotnet 6 SDK

First we will create our application

dotnet new webapi -minimal -o TodoApi
Enter fullscreen mode Exit fullscreen mode

Let us open this in Visual Studio Code and we can examin the file structure, as we can see the folder structure is way smaller then normal APIs, no more startup class or controller folders all we have is a Program.cs

Image description

Let us now Open Program.cs and clear everything from it so we can build a very simple API wi the less then 4 lines of code (Yeah its a crazy world)

The first thing we will need is to have a Web application builder

// Initialise a web application from our console application
var app = WebApplication.CreateBuilder(args).Build();

// Map the default route
app.MapGet("/", () =>