SHARE:

ASP.NET Core 6: Working with minimal APIs

Introduction

ASP.NET Core 6 is taking shape and I’m already a fan of one of its new features, the one that allows you to create APIs with the minimum dependence on the WebAPI framework and the minimum code and files necessary for the development of minimalist APIs: minimal APIs, let’s see together how does it look like.

Create a minimal API project

There is no particular ASP.NET Core template to build a minimal API like Blazor or a webAPI, you can simply use the “ASP.NET Core Empty” template to create your project:

This template generates the following minimalist project:

The Startup.cs file has disappeared, only the Program.cs file and appsettings.json files remain. The great thing is that now the Program.cs file is generated by default with the Top-level programs feature brought by C# 9 last year.

The thing to understand here is that mininal APIs is a simplified mix between:

Nothing less nothing more!

Concrete example

On the next example, I’ll implement the following features taken from my ASP.NET WebAPI here: commonfeatures-webapi-aspnetcore/Startup.cs at master · AnthonyGiretti/commonfeatures-webapi-aspnetcore (github.com):

This is minimalist but it meets, most often, requirements for a single endpoint hats needs to be documented, protected by authentication and well designed with a service that needs to be abstracted in an interface, minimalist doesn’t mean that your code should not be well designed!

Here we go:

A simple service that returns some logic depending on some parameters:

The GlobalUsing.cs file designed for the ASP.NET Core 6 minimal API:

And the Program.cs file:

Note that in ASP.NET Core 6 mininum, route-to-code endpoints can manage easily HTTP statuses with the new static class Results. It can as well manage many kind of results such as Json, File, Text etc…. All these methods return a IResult, Here is the Results static class signature from the Microsoft.AspNetCore.Http assembly 6.0.0.0:

Last thing I really liked there is the dependency injection in minimal API, services don’t need anymore to be injected with the [FromService] attribute, and even more: when you are using Authentication feature, some user Identity objects are also injected automatically such as ClaimsPrincipal in the example there.

Demo

Let’s see a quick demo!

The following example shows a BadRequest reponse:

The following shows a Ok response:

And the following shows the failed Authentication with the UnAuthenticated response:

Conclusion

This example is simple but meetsmost of time minimal requirements, I did not show all the possibilities in all the scenarios offered by the minimal APIs, but my Github repository here: https://github.com/AnthonyGiretti/aspnetcore-minimal-api will regularly evolve, at the same time as I discover the miniamal APIs. If you want to contribute you are welcome!

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.