读取配置文件
1、首先在控制器引入读配置文件对象
private readonly IConfiguration _IConfiguration; public ThirdController(IConfiguration IConfiguration) { _IConfiguration = IConfiguration; }
2、代码如下操作即可
var AllowedHosts = this._IConfiguration["AllowedHosts"]; var write = this._IConfiguration["ConnectionStrings:DbWrite"]; var write0 = this._IConfiguration["ConnectionStrings:DbWrite:0"]; var readarray = this._IConfiguration.GetSection("ConnectionStrings").GetSection("DbReads").GetChildren().Select(a => a.Value).ToArray();
配置文件内容如下:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "ConnectionStrings": { "DbWrite": "Server=.;Database=LhHt6;Persist Security Info=True;User ID=sa;password=Sasa123;MultipleActiveResultSets=true", "DbReads": [ "Server=.;Database=LhHt6;Persist Security Info=True;User ID=sa;password=Sasa123;MultipleActiveResultSets=true", "Server=.;Database=LhHt6;Persist Security Info=True;User ID=sa;password=Sasa123;MultipleActiveResultSets=true", "Server=.;Database=LhHt6;Persist Security Info=True;User ID=sa;password=Sasa123;MultipleActiveResultSets=true" ] }, "AllowedHosts": "*" }