Azure 应用服务配置未覆盖我在 appsetttings.json 中的应用设置

Azure App Service Configuration not overriding my appsettings in appsetttings.json

在配置生成器中:

.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

在AppSettings.json中:

    "AllowedHosts": "*",
    "AppSettings": {
        "Edition": "Developer"
    }

在 Azure 应用服务配置中: 我做了几次不同的尝试,但都没有成功。

  {
    "name": "AppSettings.Edition",
    "value": "Standard",
    "slotSetting": false
  },
  {
    "name": "AppSettings:Edition",
    "value": "Standard",
    "slotSetting": false
  },
  {
    "name": "AppSettings__Edition",
    "value": "Standard",
    "slotSetting": false
  },
  {
    "name": "AppSettings_Edition",
    "value": "Standard",
    "slotSetting": false
  },
  {
    "name": "Edition",
    "value": "Standard",
    "slotSetting": false
  },

I want to note that under Connection strings, I have no problem overwriting.

我做错了什么?

从 Azure 应用服务覆盖 appsettings.json 值的解决方法之一是:

家庭控制器:

private AppSettings AppSettings { get; set; }
public HomeController(IOptions<AppSettings> appSettings)
{
    AppSettings = appSettings.Value;
}
public IActionResult Index()
{
    ViewBag.Message = AppSettings.Hello;
    return View();
}

AppSettings.cs:

public class AppSettings
{
    public string Hello { get; set; }
}

Startup.cs:

public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
    Configuration = configuration;
    Configuration = new ConfigurationBuilder()
        .AddConfiguration(configuration)
        .SetBasePath(environment.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables()
        .Build();
}
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}

appsettings.json:

"AppSettings": {
    "Hello": "world"
}

在Azure门户的应用服务配置的应用程序设置中添加上述appsettings.json值。
Azure GUI(连接字符串、应用程序设置)在内部使用环境变量,因此 appsettings.json 将保持不变。 请参考此类似 了解更多信息。