.net core读取配置文件

先添加这两个开发包:

 

这是配置文件;

 

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "name": { "name": "testConfig" }
}

  

 

 

代码(.net core 6):

    ConfigurationBuilder configBuilder = new ConfigurationBuilder();
    configBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);//, reloadonChange: false);
    IConfigurationRoot config = configBuilder.Build();
    //string name = config["name"];
    //Console.WriteLine($"name={name}");
    string proxyAddress = config.GetSection("Logging:LogLevel:Default").Value;
    Console.WriteLine($"Address: {proxyAddress}");

  

控制台输出:

 

posted @ 2023-07-02 15:23  艾特-天空之海  阅读(36)  评论(0编辑  收藏  举报