Following on from a StackOverflow question regarding Using IConfiguration globally in mvc6. A comment to the accepted answer suggests using
services.Configure<SomeOptions>(Configuration);
Now this works fine with the following code;
Class
public class SomeOptions
{
public string MyOption { get; set; }
}
config.json
{
"MyOption": "OptionValue"
}
Startup.cs
public Startup(IHostingEnvironment env)
{
Configuration = new Configuration()
.AddJsonFile("config.json")
.AddEnvironmentVariables();
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SomeOptions>(Configuration);
}
However the config.json
file doesn't have any really structure, and I would like it to look more like;
{
"SomeOptions": {
"MyOption": "OptionValue"
}
}
However this does not bind the values within the class. Is there anyway to allow this?
You can access specific value in config.json like:
Which returns
So, your code will be
If you want to change the
config.json
structure you also need to change your class structure.maps to something like
Should do it.