Simplifying API Development using .NET 6 Minimal APIs

Creating a minimal API is an excellent way to build lightweight and efficient web services using .NET 6. Minimal APIs provide a clean and straightforward approach to designing APIs with minimal boilerplate code. In this blog post, we will guide you through the process of creating a minimal API using .NET 6.

What is a Minimal API?

A Minimal API is a feature introduced in .NET 6 that allows you to create HTTP APIs with minimal configuration and code. Unlike traditional ASP.NET Core applications, where you might have controllers, routing, and middleware, minimal APIs enable you to define endpoints quickly and with less ceremony.

Prerequisites

Before we get started, make sure you have the following prerequisites installed:

Step 1. Create a New Project

Let's begin by creating a new .NET 6 Minimal API project.

Start Visual Studio 2022 and select Create a new project. In the Create a new project dialog:

ASP.NET Core Web API Template

Select ASP.NET Core Web API, click on ‘Next’. Where provide Project Name and click on “Next”

Additional Information Window

On Additional Information Window, uncheck “Use controller” and click on “Create.”

This will create a new project named "MinimalAPIDemo".

Step 2. Define Your Minimal API

In a minimal API, you define your endpoints directly in the Program.cs file. Open Program.cs in your favorite code editor. You'll see a minimal setup like this:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateTime.Now.AddDays(index),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast");

app.Run();

internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

In this example, we create a simple " GetWeatherForecast " endpoint that responds to a GET request at the root "/weatherforecast"

Step 3. Run Your Minimal API

You can now run your minimal API

Swagger Page

You should see swagger indicating that your application is running.

Step 4. Test Your API

Let us test the endpoint through swagger.

API Response

You should receive weather forecast list as listed mentioned above.

Step 5. Add More Endpoints

To add more endpoints to your minimal API, you can use the app.Map* methods. For example, let's add an endpoint in Program.cs that returns the current date and time:

app.MapGet("/time", () => DateTime.UtcNow.ToString()).WithName("GetTime");

Now, you can access this endpoint at /time.

Conclusion

Creating a minimal API in .NET 6 is a straightforward and efficient way to build web services. You can easily define your endpoints without the overhead of traditional ASP.NET Core applications. As your project grows, you can add more endpoints and functionality to meet your specific requirements. Minimal APIs are a great choice for building lightweight and efficient web APIs with .NET 6.