In this post we are going to see how to get the IConfiguration in the Program class in the new ASP.NET Core 6 template.
As you may already know, in ASP.NET Core 6 the Program class is a Top-Level Statement. So we don’t have the “public class program” or anything like that (at least not explicitly).
How do we get an instance of the IConfiguration service? Like this:
// Add services to the container.
var configuration = builder.Configuration;
In this way, the configuration variable is of type IConfiguration. And so we can get a value from a configuration provider:
var value = configuration.GetValue<string>("value");
And that’s it!
Courses
Get one of my courses today (ASP.NET Core Web APIs, Blazor, and more): https://www.felipe-gavilan.com/cursos?idioma=eng
Kind regards!