ASP.NET Core appsettings.json 未加载正确的设置

Jac*_*ier 8 c# asp.net-core asp.net-core-webapi asp.net-core-configuration

在我的 Azure 中,我有ENVIRONMENT = Development,但我的设置未加载。

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
       .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
       .AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
       .Build();
Run Code Online (Sandbox Code Playgroud)

但它始终使用默认设置。

小智 3

请更新您的以下行:

.SetBasePath(env.ContentRootPath)
Run Code Online (Sandbox Code Playgroud)