0

In ConfigurationBuilder:

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

In AppSettings.json:

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

In Azure App Service Configuration: I have made several different attempts, but without success.

  {
    "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.

What am I doing wrong?

1 Answers1

0

One of the work around to override the appsettings.json values from Azure App service is :

HomeController:

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"
}

Add the above appsettings.json value in the application settings of the App service configuration of the Azure portal.
Azure GUI (Connection strings, Application settings) uses environment variables internally, so the appsettings.json will stay the same. Please refer this similar example for more information.

  • I already had most of the settings… BUT… I was missing ONE line. .AddEnvironmentVariables () And now everything works: In ConfigurationBuilder: var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables() .Build(); In Azure App Service Configuration: { "name": "AppSettings:Edition", "value": "Standard", "slotSetting": false }, Thanks – Peter Steiness Jan 31 '22 at 08:52