.NET 6 has introduced Minimal APIs for developers. It enables creation of RESTful APIs with less dependencies. These are suited for microservices and applications that need to include only few source code files, functionalities, and dependencies. Developers can create a new API endpoint in .NET 6 with just 4 lines of code.
Minimal APIs
- Minimal APIs can be created from command prompt or Visual Studio
- From command prompt, use the dotnet new web command
- From visual studio, uncheck the ‘Use controllers’ checkbox while creating a web API to create it as a minimal API
- The minimal API created will only have below 4 lines of code
- There is no need to specify the using statements explicitly with the new ‘Implicit Global Using’s’ feature of .NET 6. The using statements for all source files can be declared globally in one file.
API Endpoints and Middleware
- Unlike the ASP.NET web API, there is no controller needed to define the API endpoints. The API endpoints can be defined as route handlers directly within the program.cs file.
- Most of the middleware services that are supported in the web APIs are supported in minimal APIs too. These can be directly defined in the program.cs file.
- UseAuthentication
- UseAuthorization
- UseCors
- UseExceptionHandler
- UseForwardedHeaders
- UseHttpsRedirection
- UseHsts
- UseHttpLogging
- UseW3CLogging
- UseResponseCaching
- UseResponseCompression
- UseSession
- UseStaticFiles
- UseFileServer
- UseWebSockets
Open API
- .NET 6 Minimal APIs support Open API specification for services. The swagger configurations can be enabled as indicated in the code below.
- The swagger UI can be viewed by navigating to http://d<omainame>/swagger
Dependency Injection
- Minimal APIs make dependency injection much simpler
- The services from a DI container can be requested from the rote handlers directly without explicitly specifying the [FromServices] attribute
- In web APIs with controller, the [FromServices] attributes has to be explicitly specified
Limitations of Minimal APIs
Even though the minimal APIs help to fast-track the development of simple APIs, there are many limitations compared to web API with controllers.
- No support for Action/Authorization/Exception filters which is widely used in web APIs with controllers
- Model binding : No support for IModelBinderProvider, IModelBinder
- API versioning is not supported
- Binding from forms : No support for IFormFile
- Validation : No support for IModelValidator
- No view rendering support
- OData is not supported
Summary
.NET 6 minimal APIs is a good development choice for small microservices which only needs few endpoints. It maps http routes to an inline or defined action method. A developer just needs to start with few lines of code to get a service up and running and it provides most of the powerful features needed to build a microservice. It makes it easy to maintain the source code for simple APIs. More features might get added to minimal APIs with new framework versions. Even though minimal APIs support many features, it would be better to use web APIs for more complex APIs which need to serve many endpoints.
Looking forward for your comments.