Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change a setting in appsettings.json after auto-deploy?

I have an ASP.NET Core application going on an have setup Github auto-deploy on it. But since it's an open repo I obviously don't want to upload my correct configuration file.

What I'd like to do is to replace some strings in the appsettings.json after a github auto deploy.

"AppSettings": {
    "Token": "my super duper secret token"
}

How can I change my super duper secret token to my real token after a github deploy on Azure?

like image 342
Ms01 Avatar asked Oct 23 '16 19:10

Ms01


People also ask

Can we update Appsettings json?

json programmatically. You have to overwrite the appsettings. json file to be able to update values programmatically.

Does APP service configuration override Appsettings json?

For ASP.NET and ASP.NET Core developers, setting app settings in App Service are like setting them in <appSettings> in Web. config or appsettings. json, but the values in App Service override the ones in Web.

Can you change a connection string after you deploy your Web application to Azure Web app service?

Navigate to the Azure App Service Web App within the portal. Under Settings open up the Configuration option. The Connection strings section can be used to manage the settings for the application.


2 Answers

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.

  1. Create an Asp.net core Application.
  2. Add [AppSettings] section in the appsetting.json file (Token vaule: mysecretkey). enter image description here
  3. Add a public class AppSettings.cs under the created project. enter image description here
  4. 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));

enter image description here 5. Add code to the HomeController.cs file. enter image description here

  1. Publish the WebApp to the Azure Portal.
  2. Add [AppSettings: Token] in the Azure Portal. enter image description here
  3. Browse the WebApp and select the about tab to see the token value is that the value set in the portal. enter image description here
like image 108
Tom Sun - MSFT Avatar answered Oct 17 '22 07:10

Tom Sun - MSFT


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.

like image 40
Russell Young Avatar answered Oct 17 '22 06:10

Russell Young