How can I change a setting in appsettings.json after auto-deploy?
If you are using Azure DevOps Release to deploy, you can easily specify properties for each environment/stage.
You can use the task File Transform and indicate the path to appsettings.json
:
Or if you are deploying directly to Azure:
So you just need to create the variables to override the data in the settings:
As I know we can config token in App Settings on the Azure port. I do a test on this, it works successfully, the following is my detail steps.
- Create an Asp.net core Application.
- Add [AppSettings] section in the appsetting.json file (Token vaule: mysecretkey).
- Add a public class AppSettings.cs under the created project.
- Add the code
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"))
in the function ConfigureService function in the Startup.cs file (For .net Core 1.0).
Note:The syntax for model binding has changed from RC1 to RC2. Using
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"))
, is no longer availableIn order to bind a settings class to your configuration you need to configure this in the ConfigureServices method of Startup.cs:services.Configure<AppSettings>(options => Configuration.GetSection("AppSettings").Bind(options));
5. Add code to the HomeController.cs file.
- Publish the WebApp to the Azure Portal.
- Add [AppSettings: Token] in the Azure Portal.
- Browse the WebApp and select the about tab to see the token value is that the value set in the portal.
Assuming the web site already exists as a resource in Azure, you can simply set the App Settings/Connection strings in the portal. These will override the ones in the appsettings.json file at runtime. Ie. your app will first look at the azure app settings/connection strings before looking for them in the local file. This is part of asp.net core's "cloud first" approach to configuration management. These settings wont get overwritten when you deploy code to the app/slot.
Found a blog post here which describes it in a bit more detail, using the .AddEnvironmentVariables()
call to add azure slot settings to the configuration.