Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Configuration Values in config.json file in Azure Web App in ASP.Net 5

I have a ASP.Net 5 application where i have some configuration values stored in config.json file. my config.json file is something like this.

{
  "AppSettings": {
    "SiteEmailAddress": "[email protected]",
    "APIKey": "some_api_key"
  }
}

I am setting up the config.json file to use in 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 accessing the config settings like this..

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

Earlier in ASP.Net we can use the Web.Config file to store these Application Settings and override them in App Settings in Azure App Settings section and that works with out any issues. But how can i do the same thing in ASP.Net 5 app.

How can i override the configuration values in config.json file in the App Settings section in Azure.

like image 571
Kasun Kodagoda Avatar asked Nov 22 '15 13:11

Kasun Kodagoda


People also ask

Does Azure app settings override Appsettings json?

When you add, remove, or edit app settings, App Service triggers an app restart. 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.

How do I change the connection string in Azure App Service?

Connection String within the Azure Portal Open the Azure Portal via https://portal.azure.com. 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.

What is Appsetting json in ASP.NET Core?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.


1 Answers

Assuming you have appsettings.json, you could add another file appsettings.{Environment}.json, ie appsettings.Production.json. Only settings, defined in the production file would override settings in the appsettings.json. Now, add the following to the constructor of Startup

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

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

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

Now deploy to azure.

like image 65
Boris Lipschitz Avatar answered Oct 05 '22 15:10

Boris Lipschitz