This repository was created as an intro/example of Minimal APIs. Minimal APIs were introduced with the release of .NET6 as a alternative approach for building API's compared to the MVC/Controller approach.
A quote from taken from the Microsoft doc Tutorial: Create a minimal web API with ASP.NET Core defines a Minimal API as the following:
Minimal APIs are architected to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.
- Hello World - A simple hello world example project.
This project demonstrates how simple it is to get going with minimal apis in a hello world example project.
- Comparison - Minimal API vs MVC/Controller API project comparison.
This section contains the following two projects
- Examples.MinimalApi.WeatherForecast - an example of the default project that gets created when creating minimal api
- Examples.MVC.WeatherForecast - an example of the default project when using the MVC approach for building a api
NOTE: These projects are not mutually exclusive. You can use a mixture of both approaches in one project, but if you introduce the MVC approach with a minimal api you forgo the performance improvements that come with the removal of Controllers.
- Structure - Todo Minimal API's structured in various ways with some examples using popular libraries.
This section contains three example todo API projects that demonstrate the various ways you can create minimal apis created with popular libraries:
- Examples.MinimalApi.Todo.EFCore - a simple CRUD example api built with the EFCore package.
- Examples.MinimalApi.Todo.MediatR - similar to the previous project with the introduction of MediatR package, demonstrating one possible way of structuring a minimal api using a popular library.
- Examples.MinimalApi.Todo.FastEndpoints - another example todo api project structured using the FastEndpoints package. A light-weight REST Api framework for ASP.Net 6 that implements REPR (Request-Endpoint-Response) Pattern.
- Testing - Unit tests and integration tests for Minimal APIs.
This sections has two projects that gives examples of testing a minimal api. These test projects were created for the Examples.MinimalApi.Todo.FastEndpoints project.
- Examples.MinimalApi.Todo.FastEndpoints.UnitTests - example of unit testing a minimal api.
- Examples.MinimalApi.Todo.FastEndpoints.IntegrationTests - example of integration tests for a minimal api.
- Top-level statements: Introduced with C# 9 you don't have to explicitly include a
Main
method. TheMain
method is implied, it is implicitly there.
// Before C# 9
class TestClass
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
// Introduced in C# 9
Console.WriteLine("Hello World!");
- Global and implicit usings: Introduced in C# 10, implicit usings feature automatically adds common global using directives.
// format: global using <fully-qualified-namespace>;
// applies to the entire project
global using System;
global using static System.Console;
global using Env = System.Environment;
- Improvements for lambda expressions: Another feature introduced in C# 10 implicit lambda expressions.
// Before C# 10
Func<string, int> parse = (string s) => int.Parse(s);
// Introduced in C# 10
var parse = (string s) => int.Parse(s);
- Attributes on lambdas: In the same way you could put attributes to methods or local functions you can you put them on lambdas, Also introduced in C# 10.
Func<string, int> parse = [Example(1)] (s) => int.Parse(s);
var choose = [Example(2)][Example(3)] object (bool b) => b ? 1 : "two";