.netcore3.1读取配置文件
作者:互联网
读取配置文件
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": "*" }
标签:LhHt6,读取,配置文件,IConfiguration,Persist,sa,ID,netcore3.1 来源: https://www.cnblogs.com/daiyekun-blog/p/13958925.html