首页 > 解决方案 > ASP.NET Core appsettings.json 未加载正确的设置

问题描述

在我的 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();

但它始终使用默认设置。

标签: c#asp.net-coreasp.net-core-webapiasp.net-core-configuration

解决方案


请更新您的以下行:

.SetBasePath(env.ContentRootPath)

推荐阅读