c# - Is there a built in way to serialize array configuration values? -
i'm on new asp.net 5 project.
i'm trying read array value, stored in config.json
file, looks this:
{ "appsettings": { "sitetitle": "myproject", "tenants": { "reservedsubdomains": ["www", "info", "admin"] } }, "data": { "defaultconnection": { "connectionstring": "server=(localdb)\\mssqllocaldb;database=aspnet5-myproject....." } } }
how access c# code?
at least beta4 arrays aren't supported in config.json
. see asp.net issue 620. use following config.json
:
"appsettings": { "sitetitle": "myproject", "tenants": { "reservedsubdomains": "www, info, admin" } }
and map class this:
public class appsettings { public string sitetitle { get; set; } public appsettingstenants tenants { get; set; } = new appsettingstenants(); } public class appsettingstenants { public string reservedsubdomains { get; set; } public list<string> reservedsubdomainlist { { return !string.isnullorempty(reservedsubdomains) ? reservedsubdomains.split(',').tolist() : new list<string>(); } } }
this can injected controller:
public class mycontroller : controller { private readonly appsettings _appsettings; public mycontroller(ioptions<appsettings> appsettings) { _appsettings = appsettings.options; }
Comments
Post a Comment