.net core Program.cs中读取appsettings.json路径读取错误问题

在.net core 3.1中遇到读取appsettings.json路径读取错误问题.

在.net core 3.1中使用以下语句读取,其中第一条验证可用,其他未验证过。3.1比较特殊,其他版本似乎没有这个问题.

1.验证可用

var configuration = new ConfigurationBuilder()
                       .SetBasePath(Environment.CurrentDirectory)
                       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                       .Build();

2.

 IConfiguration configuration = new ConfigurationBuilder()
            .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddJsonFile("appsettings.json")
            .Build();

3.

// 读取 appsettings.json 文件  
var builder = new ConfigurationBuilder()  
    .SetBasePath(Directory.GetCurrentDirectory())  
    .AddJsonFile("appsettings.json");  
  
var config = builder.Build();  
  
// 读取配置项  
var value = config["section:key"];

4.

 var configuration = new ConfigurationBuilder()
         .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))     
         .AddJsonFile("appsettings.json")
         .Build();       

5.

 var configuration = new ConfigurationBuilder()
        .SetBasePath(AppContext.BaseDirectory)     
        .AddJsonFile("appsettings.json")
        .Build();

你可能感兴趣的:(.net,.netcore)