Simple ASP.NET Core Minimal API

Introduction

This article shows how to create an ASP.Net core minimal API using .Net 6 and Visual Studio 2022. This article starts with an introduction of the minimal APIs, after that it demonstrates how to add model and HTTP action methods to get and post data to API. In the end, the article discusses the differences between minimal API and traditional web API.

Creating the project

I will be using Visual Studio 2022 for this article but you can use VS 2019 with .Net 6 SDK installed. If you are using VS 2022 you can follow this article without any problem. With that being said follow steps to create a new web API project,

  • Open Visual Studio 2022.
  • Click on "Create a New Project".
  • Select "ASP .Net Core Web API" template from the list and click Next.
  • Name your project whatever you want, I will name this project "MoviesMinimalApi". Click Next.
  • This tutorial is for minimal APIs. We will not be discussing anything else like Https or OpenApi support. So uncheck "Configure for HTTPS" and "Enable OpenApi Support".
  • Also, uncheck "Use Controllers (uncheck to use minimal APIs)". Because we want to work with minimal APIs.
  • Click "Create".

The project will be created with a basic minimal API. Open file Program.cs. You will find the same code as in Listing 1:

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var app = builder.Build();
// Configure the HTTP request pipeline.
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;
});
app.Run();
internal record WeatherForecast(DateTime Date, int TemperatureC, string ? Summary) {
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

Listing 1

Remove all the code from Program.cs file and replace it with the following code,

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/helloworld", () => {
    return "Hello world...";
});
app.Run();

Build and run the project and in URL change "weatherforecast" to "helloworld". You will see "Hello World..." printed on the screen. So far so good.

Adding the model

Now we will add a sample model so that we can get, post, update or delete data. In the Program.cs file under app.,Run() add following code,

public class Movie {
    public int Id {
        get;
        set;
    }
    public string ? Name {
        get;
        set;
    }
    public string ? Genre {
        get;
        set;
    }
    public decimal Price {
        get;
        set;
    }
}

Listing 2

Adding Sample Movie Data

Just under var app = builder.Build();

add following code,

var movies = new List<Movie>();
movies.Add(new Movie { Id = 1, Name = "The godfather", Genre = "Crime", Price = 25 });
movies.Add(new Movie { Id = 2, Name = "The fault in our stars", Genre = "Romance", Price = 20 });
movies.Add(new Movie { Id = 3, Name = "Battleship", Genre = "Action", Price = 15 });
movies.Add(new Movie { Id = 4, Name = "Due date", Genre = "Drama", Price = 30 });
movies.Add(new Movie { Id = 5, Name = "The big short", Genre = "Documentary", Price = 40 });

The code for the HTTP action methods for get, post, put and delete is given below,

app.MapGet("/getallmovies", () => {
    return movies;
});
app.MapPost("", (Movie movie) => {
    movies.Add(movie);
    return movies;
});
app.MapPut("", (Movie movie) => {
    var index = movies.FindIndex(m => m.Id == movie.Id);
    movies[index] = movie;
    return movies;
});
app.MapDelete("/{id}", (int id) => {
    var index = movies.FindIndex(m => m.Id == id);
    movies[index] = null;
    return movies;
});

Now, open postman and test the APIs. You can post and update movies using body of postman. For deleting the movie you can send id as part of route like "localhost:Your_Port/{id}". The Program.cs file as a whole should look like this,

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var movies = new List < Movie > ();
movies.Add(new Movie {
    Id = 1, Name = "The godfather", Genre = "Crime", Price = 25
});
movies.Add(new Movie {
    Id = 2, Name = "The fault in our stars", Genre = "Romance", Price = 20
});
movies.Add(new Movie {
    Id = 3, Name = "Battleship", Genre = "Action", Price = 15
});
movies.Add(new Movie {
    Id = 4, Name = "Due date", Genre = "Drama", Price = 30
});
movies.Add(new Movie {
    Id = 5, Name = "The big short", Genre = "Documentary", Price = 40
});
app.MapGet("/helloworld", () => {
    return "Hello world...";
});
app.MapGet("/getallmovies", () => {
    return movies;
});
app.MapPost("", (Movie movie) => {
    movies.Add(movie);
    return movies;
});
app.MapPut("", (Movie movie) => {
    var index = movies.FindIndex(m => m.Id == movie.Id);
    movies[index] = movie;
    return movies;
});
app.MapDelete("/{id}", (int id) => {
    var index = movies.FindIndex(m => m.Id == id);
    movies[index] = null;
    return movies;
});
app.Run();
public class Movie {
    public int Id {
        get;
        set;
    }
    public string ? Name {
        get;
        set;
    }
    public string ? Genre {
        get;
        set;
    }
    public decimal Price {
        get;
        set;
    }
}

Listing 3

Summary

In this article, I discussed how to create a simple minimal API. Microsoft has introduced minimal APIs in .Net 6. They are extremely useful for creating microservices. You can create a microservice very fast with very few lines of code and deploy it. That said there are also several limitations to using minimal APIs. For example, you can't upload files using minimal APIs for that you still have to use traditional web APIs. You can't also use advance model binding as you use in Asp .Net Core Web API or MVC or Razor Pages. With that being said for limited use Minimal APIs are best to use. Microsoft has also promised to increase the functionality of minimal APIs in the future.