Getting the IConfiguration in the Program class – ASP.NET Core 6

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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s