appsettings.json
{
"MyConfigValues": {
"Hello": "My Values"
}
}
My Data Holding Class
public class MyConfigModel
{
public string Hello { get; set; }
}
Startup.cs
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<MyConfigModel>(options => Configuration.GetSection("MyConfigValues").Bind(options));
// Alternative:
services.AddOptions<MyConfigModel>()
.Bind(Configuration.GetSection("MyConfigValues"));
...
}
...
}
Controller
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
private readonly IOptions<MyConfigModel> config;
public TestController(IOptions<MyConfigModel> config) => this.config = config;
[HttpGet]
public ActionResult<MyConfigModel> Get() =>
Ok(this.config.Value);
}