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:
2, Give the related service principal required permission:
(Note: The role assign need 5 minutes to apply, please wait.)
3, Use DevOps pipeline to change the app settings:
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:
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
Differencebetween a Property and a Variable
How to Read Data from a Zip File Without Having to Unzip the Entire File
Failed to Serialize the Response in Web API
What Is an MVChtmlstring and When Should I Use It
What Are the Downsides to Turning Off Proxycreationenabled for Ctp5 of Ef Code First
Excel to Datatable Using Epplus - Excel Locked for Editing
Linq to Entities Only Supports Casting Edm Primitive or Enumeration Types with Ientity Interface
What Is the Algorithm to Convert an Excel Column Letter into Its Number
Why We Need Thread.Memorybarrier()
One to One Optional Relationship Using Entity Framework Fluent API
How to Copy Part of an Array to Another Array in C#
Deleting Specific Rows from Datatable
Signing and Verifying Signatures with Rsa C#
Populate Data Table from Data Reader
Windows Shell Extension with C#