How to Update Values into Appsetting.JSON

How to update appsettings.json value using Azure

From this document:

https://learn.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal#configure-app-settings

the values of configuration in App Service override the ones in Web.config or appsettings.json.

So, just need to change to configuration settings is ok.

How to do it in Azure pipeline:

1, Create a service connection:

Sample Image

Sample Image

2, Give the related service principal required permission:

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

(Note: The role assign need 5 minutes to apply, please wait.)

3, Use DevOps pipeline to change the app settings:

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

If you use YAML, it is

steps:
- task: AzureAppServiceSettings@1
displayName: 'xxx'
inputs:
azureSubscription: xxx
appName: xxx
resourceGroupName: 'xxx'
appSettings: |
[
{
"name": "bowmantest",
"value": "bowmantestvalue",
"slotSetting": false
}
]

4, After that, just wait, it will be successful:

Sample Image

ASP.NET Core appsettings.json update in code

Here is a relevant article from Microsoft regarding Configuration setup in .Net Core Apps:

Asp.Net Core Configuration

The page also has sample code which may also be helpful.

Update

I thought In-memory provider and binding to a POCO class might be of some use but does not work as OP expected.

The next option can be setting reloadOnChange parameter of AddJsonFile to true while adding the configuration file and
manually parsing the JSON configuration file and making changes as intended.

    public class Startup
{
...
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
...
}

... reloadOnChange is only supported in ASP.NET Core 1.1 and higher.

How to change values in appSettings.json file based on environment

The default of ConfigurationBuilder is looking for appsettings.<EnvironmentName>.json file, so based on the environment that you are working with

when you are in IIS Express you are in Development and when you deploy your application your environment is Production. This is why you need appsettings.Production.json



Related Topics



Leave a reply



Submit