To use and access appsettings in .NET 7 minimal API you can use the builder variable. It’s the first thing in Program.cs that is created.
var builder = WebApplication.CreateBuilder(args);
To access the appsettings use the Configuration and the getvalue() function like this.
var Id = builder.Configuration.GetValue<string>("UniqueId");
The appsettings.json file will look something like this. Add your unique string you want to access.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"UniqueId": "103d651f-e00c-4e98-b4bd-3ab20b76be95",
"AllowedHosts": "*"
}
You can also add other settings like connection strings, environment variables, or anything else that you need to access in order to run your application.
Source document for minimal api https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-7.0