Using configuration options in .NET 6 (.NET core)

.NET

Assume you have the following data in your appsettings.json


{
	"MyData":
	{
		"BaseUrl": "https://example.com"
	}
}

Create a class to map these settings


public class MyData 
{
	public string? BaseUrl { get; set;}
}

Register in the DI


builder.Services.AddOptions().Configure<MyData>(builder.Configuration.GetSection("MyData"));

Inject in controller (or wherever)


IOptions<MyData> settings

Post a Comment

Previous Post Next Post