Skip to content

An ASP.NET Core project, that uses .NET 7 to demonstrate how to create a minimal Api; with integrations with EntityFramework 7 and StackExchange.Redis to store objects in an Azure Cache for Redis cache.

Notifications You must be signed in to change notification settings

romayneeastmond/application-net7-minimal-api-redis

Repository files navigation

.NET 7 Minimal Api, EntityFramework 7, and StackExchange.Redis

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.

Minimal Api Examples
The MapGroup Extension Method
var group = app.MapGroup("/api").AddEndpointFilter(async (context, next) =>
{
    return await next(context);
});
AsParametersAttribute Type Decorator
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);
};
Traditional Object Binding
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();
};

How to Use

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.

Generate Migrations

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

Copyright and Ownership

All terms used are copyright to their original authors.

About

An ASP.NET Core project, that uses .NET 7 to demonstrate how to create a minimal Api; with integrations with EntityFramework 7 and StackExchange.Redis to store objects in an Azure Cache for Redis cache.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages