Overriding configuration values ​​in config.json file in Azure Web App in ASP.Net 5

I have an ASP.Net 5 application where I have some configuration values ​​stored in config.json file. My config.json file looks something like this.

 { "AppSettings": { "SiteEmailAddress": "some@email.com", "APIKey": "some_api_key" } } 

I am setting up a config.json file for use in a Startup.cs file like this.

 public static IConfigurationRoot Configuration; public Startup(IApplicationEnvironment appEnv) { var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath) .AddJsonFile("config.json") .AddEnvironmentVariables(); Configuration = builder.Build(); } 

And access to configuration settings like this.

 var email = Startup.Configuration["AppSettings:SiteEmailAddress"]; 

Earlier in ASP.Net, we could use the Web.Config file to store these application settings and override them in the application settings in the "Azure Application Settings" section, and this fixes any problems. But how can I do the same in an ASP.Net 5 application.

How to override configuration values ​​in config.json file in the "Application Settings" section in Azure.

+8
c # asp.net-core azure
source share
2 answers

Add them as application settings in Azure, just like you. For nested configuration values ​​use

 AppSettings:SiteEmailAddress 

Etc ... (AppSettings here, referring to what you used in your config.json, the resemblance to the settings of the Azure application is a coincidence)

AddEnvironmentVariables (), as you did, is required for this.

+19
source share

Assuming you have appsettings.json, you can add another appsettings file. {Environment} .json, i.e. appsettings.Production.json. Only the settings defined in the working file will override the settings in appsettings.json. Now add the following to the Startup constructor

 var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true); 

Then you should go to launchSettings.json where all the servers are defined and update the environment variable for Production. For example,

 "web": { "commandName": "web", "environmentVariables": { "Hosting:Environment": "Production" } 

Now deploy to azure.

0
source share

All Articles