The repo shows how .NET 6 is used to create APIs.
Before running the sample code, download the REST Client for testing the APIs.
This is a new feature in .NET 6 that allows developers to skip a lot of the boilerplate code used to create .NET Web APIs. Seeing an API without the extra plumbing should help in understanding the middleware and controllers used in traditional .NET Web APIs.
- Switch to the
1-minimal-api
branch in VS Code. - Start the API by running
dotnet run
. - Test the API by using the tests/test.http file.
This shows the standard way to configure an API prior to .NET 6. It uses the Middleware pipelines, route configurations, and controllers.
- Switch to the
2-standard-api
branch in VS Code. - Start the API by running
dotnet run
. - Test the API by using the tests/students.http and tests/home.http files.
- Examing the program.cs file and note two things
- use of the
builder
to configure common and shared runtime services for all endpoints in the API - use of the
app
to configure the middleware stack the gets executed on each request.
- use of the
This shows how to design and implement an API with .NET 6
We need to add support for a local database to solve for the stateless issue. We will use Enity Framework's In-Memory database.
dotnet add package Microsoft.EntityFrameworkCore.InMemory
We begin by creating the models of our business entities. This is usually done with business subject matter experts as well as teams that will consume our API
We will not spend time here- this is Wednesday lecture.
- Add the following Nuget packages:
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet tool install -g dotnet-aspnet-codegenerator
- Then generate a CoursesController based on the model: -
dotnet aspnet-codegenerator controller -name CoursesController -async -api -m Course -dc RegistrarContext -outDir Controllers