老式的config文件在ASP。net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"Wizards": [
{
"Name": "Gandalf",
"Age": "1000"
},
{
"Name": "Harry",
"Age": "17"
}
],
"Sex": {
"Man": 0,
"Female": 1
}
}
读取的时候,比如读取Man的值,可以这么写:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var config = builder.Build();
var ret = config.GetSection("Sex:Man").Value;
其他更多配置,例如数据库等请参考:
https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-mvc-app/working-with-sql?tabs=aspnetcore2x
和
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/index?tabs=basicconfiguration