.net core 3.0 读取appsettings.json(推荐)

引用:microsoft.extensions.configuration

          microsoft.extensions.configuration.json

 1 public iactionresult index()
 2 {
 3     var builder = new configurationbuilder()
 4         .setbasepath(directory.getcurrentdirectory())
 5         .addjsonfile("appsettings.json")
 6         .addjsonfile("appsettings.test.json", true, reloadonchange: true);
 7  
 8     var config = builder.build();
 9  
10     //读取配置
11     viewdata["secret"] = config["appsettings:key"];
12     return view();
13 }

 .Net 5下亲测

1     public string GetAppsetting(string key)
2         {
3             var builder = new ConfigurationBuilder()
4             .SetBasePath(Directory.GetCurrentDirectory())
5             .AddJsonFile("appsettings.json")
6             .AddJsonFile("appsettings.test.json", true, reloadOnChange: true);
7             var config = builder.Build();
8             return config[key]; //读取配置
9         }

 

 

 

 

转自:http://www.10qianwan.com/articledetail/406420.html

posted @ 2020-07-12 20:48  PrintY  阅读(386)  评论(0编辑  收藏  举报