ASP.NET Core 6: Working with minimal APIs
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:
- C# 9 Top-level programs feature
- Route-to-code feature brought in ASP.NET Core 3: Nano services with ASP.NET Core or how to build a light API – Anthony Giretti’s .NET blog
- ASP.NET Core application configuration moved from the Startup.cs file to Program.cs file
Nothing less nothing more!
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):
- Swagger documentation
- Endpoint protected by authentication with JWT
- Dependency injection without using [FromService] attributes
- C# 9 & 10 features such as Top-level programs, Global usings, File-scoped namespaces
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 126.96.36.199:
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.
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:
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!