相关文章推荐
很酷的生姜  ·  "Mostly Harmless ...·  5 月前    · 
发财的青椒  ·  Registered driver ...·  11 月前    · 
爱旅游的荔枝  ·  Help with error on ...·  1 年前    · 
痴情的牛肉面  ·  计时器 - 知乎·  1 年前    · 
加载配置文件
var initData = new List&ltKeyValuePair&ltstring,string&gt&gt();
initData.Add(new KeyValuePair&ltString, string&gt("username", "niewei");
IConfiguration configuration = new ConfigurationBuilder().SetBasePath(Enviroment.CurrentDirectory)
.AddJsonFile("appsettings.json")
.AddXmlFile("appsettings.xml")
.AddInMemoryCollection(initData)
.Build();
var info = configuration["username"];
如果同名,后面的覆盖前面的。

读取配置的方式
{
"mysql": {
"host":"192.168.0.1",
"port":3306
},
"ids": [10, 20, 30, 40]
}
弱类型方式
1、":"运算符, configuration["mysql:ids:0"]
2、GetSection, configuration.GetSection("mysql").GetSection("ids").GetSection("0").Value
强类型方式
1、GetValue, configuration.GetValue("mysql:port")
2、Bind、Get映射为实体类
Rootobject rootobject = new Rootobject();
configuration.Bind(rootobject);

var rootobject = configuration.Get();

另:Visual Studio打开Json文件并选中所有内容复制,在需要创建类的地方,选择菜单【编辑】【选择性粘贴】【将JSON粘贴为类】自动创建类定义。