An ASP.NET Core project, created in Visual Studio 2022 v17.4.0, that uses .NET 7 Standard Term Support (STS) to demonstrate how to create a minimal Api. The project separates the concerns into the minimal Api layer, the database model layer, and then the business services layer.
Although the Program.cs file can be built without being opininated, there is more flexibility and readability with organizing the project this way.
There are examples below that also demonstrate newer functionality included with this update of .NET 7. In particular the grouping of Api endpoints is achieved by using method groups instead of lambda expressions. There is also an abilty to define method parameters as distinct, primitive types or as objects.
var group = app.MapGroup("/api").AddEndpointFilter(async (context, next) =>
{
return await next(context);
});
group.MapPost("/insert/page", Insert);
static async Task<IResult> Insert(ISitesService sitesService, [AsParameters] Page page)
{
await sitesService.InsertPage(page);
return Results.Created($"/pages/{page.Id}", page);
};
group.MapPost("/set/cache", Set);
static async Task<IResult> Set(ICacheService cacheService, string key, ResultViewModel value)
{
await cacheService.Set(key, JsonConvert.SerializeObject(value));
return Results.Ok();
};
This project uses the Visual Studio 2022 v17.4.0 which should include the packages for .NET 7 Standard Term Support (STS). Restore any necessary NuGet packages before building or deploying.
Ensure that the connection strings in the appsettings files are changed to point to the applicable instance. Then (optionally) generate or run the migrations found below.
Although Redis is not absolutely necessary for this demonstration; optionally, ensure that the settings in the appsettings.json point to a valid Redis endpoint.
This project uses Swagger to describe the available mimimal Api endpoints.
This project contains an extension method called SiteDbContextEnableMigrations that automatically runs any migrations on application start. This is ideal within a production environment. To create and apply migrations, manually, use the commands below.
Use the commands below to manually add and run
Add-Migration InitialDatabase -Context SitesDbContext
Update-Database
All terms used are copyright to their original authors.