9

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.

2 Answers 2

22

Add them as App Settings in Azure, just like you are used to. For nested config values, use

AppSettings:SiteEmailAddress

Etc... (AppSettings here referring to what you used in your config.json, the similarity to Azure App Settings is coincidental)

AddEnvironmentVariables() like you have done is required for this to work.

Sign up to request clarification or add additional context in comments.

1 Comment

Also called appsettings.json in .NET Core.
2

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.

4 Comments

Is there a way to not have to change the Hosting:Environment every time we deploy? Can we make this permanent?
you should define "profile" for each environment in launchSettings.json file. Each profile should have it's own "Hosting:Environment". ie for IIS Express => Development, for web => Production
I see. But how Azure know to pick the "web" profile? What if I'd like to have an "Azure Production" profile?
It's probably defined in the msbuild script in VS. You 'd need to lookup how to edit it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.