.net core Configuration的 索引器的隐藏技能

...

namespace Test20190417.Mvc {
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
services.Configure<CookiePolicyOptions>(options => {
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
System.Console.WriteLine(this.Configuration["dbcon"]);
System.Console.WriteLine(this.Configuration["a:b:c:d:1"]);
System.Console.WriteLine(this.Configuration["a:b2:c2:e:0:e2"]);
services.AddDbContext<selfmanagementdb>(op => op.UseSqlite(this.Configuration["dbcon"]));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

...

appsettings.json 文件内容如下:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"dbcon": "Data Source=mydb.db;Version=3;",
"a": {
"b": {
"c": { "d": [ 1, 2, 3 ] }
},
"b2": {
"c": {
"d": [ 4, 5, 6 ],
"d2": [ 7, 8, 9 ]
},
"c2": {
"d": [ 1, 3, 5 ],
"e": [
{
"e1": true,
"e2": "aaa"
},
{
"e1": true,
"e2": "aaa"
}
]
}
}
}
}

------------------------------------------------结果:

Data Source=mydb.db;Version=3;
2
aaa

任何复杂 的json结构 都可以通过 这种简单的字符串表达规则找到,非常方便!

原文地址:https://www.cnblogs.com/ProjectDD/p/10748015.html