First of all, let’s see what are we going to achieve? Let’s say we have an dotnet 9 api application.
Its appsettings.json
looks like:
"Color": "Color from appsettings",
"Person": {
"Name": "Name from appsettings"
}
Let’s try to retrieve these values:
app.MapGet("/", (IConfiguration config) =>
{
string color = config.GetSection("Color").Value ?? "No color";
string name = config.GetSection("Person:Name").Value ?? "No name";
return Results.Ok(new
{
color,
name,
message = "Hello there..."
});
});
If you visit this endpoint, you will see this output:
[Read More]