setting.json
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"wwwroot"
]
}
引用Nuget:Microsoft.Extensions.Configuration和Microsoft.Extensions.Configuration.Json
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) //SetBasePath设置配置文件所在路径
.AddJsonFile("setting.json");
var configRoot = builder.Build();
var compilerOptionsvalue =configRoot.GetSection("compilerOptions").GetSection("noImplicitAny").Value;
Console.WriteLine(compilerOptionsvalue);
//False
var excludevalue = configRoot.GetSection("exclude").GetChildren();
foreach (var item in excludevalue)
{
Console.WriteLine(item.Value);
}
//"node_modules","wwwroot"
Console.Read();
}
参考:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/index?view=aspnetcore-2.1
简单方式
json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"wwwroot"
],
"wizerd": [
{ "Name": "tangsan","Age":30 },
{ "Name": "niuchacha","Age":22 }
]
}
读取方式:
Console.WriteLine($"Default = {this.Configuration["Logging:LogLevel:Default"]}");
Console.WriteLine($"AllowedHosts = {this.Configuration["AllowedHosts"]}");
Console.WriteLine($"noImplicitAny = {this.Configuration["compilerOptions:noImplicitAny"]}");
Console.WriteLine($"target = {this.Configuration["compilerOptions:target"]}");
Console.WriteLine($"exclude[0] = {this.Configuration["exclude:0"]}");
Console.WriteLine($"exclude[1] = {this.Configuration["exclude:1"]}");
Console.WriteLine($"wizerd[0].Name = {this.Configuration["wizerd:0:Name"]}");
Console.WriteLine($"wizerd[1].Name = {this.Configuration["wizerd:1:Name"]}");

转自:http://Www.CnBlogs.Com/WebEnh/
如果想下次快速找到我,记得点下面的关注哦!
| 本博客Android APP 下载 |
![]() |
| 支持我们就给我们点打赏 |
![]() |
| 支付宝打赏 支付宝扫一扫二维码 |
![]() |
| 微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!




浙公网安备 33010602011771号