Skip to content

Latest commit

 

History

History
86 lines (65 loc) · 5.9 KB

README.md

File metadata and controls

86 lines (65 loc) · 5.9 KB

Overview

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.

Contents

  1. 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.

  1. Comparison - Minimal API vs MVC/Controller API project comparison.

This section contains the following two projects

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.

  1. 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:

  1. 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.

Features that make Minimal APIs possible:

  • Top-level statements: Introduced with C# 9 you don't have to explicitly include a Main method. The Main 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!");
// format: global using <fully-qualified-namespace>;
// applies to the entire project

global using System;

global using static System.Console;
global using Env = System.Environment;
// 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";

Useful links