How you access confguration has changed quite a bit in NetCore from Umbraco v8. Configuration now follows the Configuration is ASP.NET Core pattern.
Principally all config is code first by convention, and configuration is stored as json in appsettings.json
file.
the final appsettings.json
file is actually built from a number of files depending on environment eg. you might have a appsettings.development.json
file that overrides settings while on your dev setup
At the basic level you might be use to getting settings from the AppSettings section of your web.config
file.
Getting application settings via Configmanager.
public string GetSomeSetting() {
return ConfigurationManager.AppSettings["MySetting"];
}
Dependency Injection is needed to get the global config
assuming you have your own object in the appsettings.json
file:
{
"MySuperApp" : {
"MySetting" : "DoSuperThings"
}
}
public class MySuperClass {
private readonly IConfiguration _configuration;
public MySuperClass(IConfiguration configuration)
{
_configuration = configuration;
}
public string GetSomeSetting() {
return _configuration["MySuperApp:MySetting"];
}
}
For more complex settings you may wish to load your own config.
In v8 this is possible but a bit more involved. you would typically have some classes to read config files load items into some class which you would then load up into the core config object during composition.
For details see how uSync8 loads up its config from file
thankfully this is much simpler. you custom config lives in appsettings.json
{
"MyCustomConfig" : {
"Enabled" : true
}
}
public class MyCustomConfig {
public static string ConfigSectionName = "MyCustomConfig";
public bool Enabled { get;set;} = false;
public bool MagicNumber {get;set;} = 10;
}
See this in DoStuff.Core/Controllers/DoStuffApiController.cs
Note this isn't the only option - see Microsoft docs for other ways to load this info.
public class MyCustomComposer : IUserComposer
{
public void Compose(IUmbracoBuilder builder)
{
// Add Options
builder.Services.Configure<MyCustomConfig>(builder.Config.GetSection(MyCustomConfig.ConfigSectionName));
}
}
public class DoStuffApiController : UmbracoAuthorizedApiController
{
private readonly MyCustomConfig _options;
public DoStuffApiController(IOptions<MyCustomConfig> options)
{
_options = options.Value;
_doStuffService = doStuffService;
}
}
public GetMagicNumber() {
if (_options.Enabled) return _options.MagicNumber;
return -1;
}