1

I have a Visual Studio 2015 solution made up of projects targeting DNX framework. I have been working locally but I plan to deploy to Azure environments (dev/test/prod). Naturally, the solution uses different database connection strings and other variables dependent on the environment. In the past I made use of cloud configuration files to set these variables, reading them with the CloudConfigurationManager.

I am given to understand that I need to use a config.json file. I have the following code in my Startup.cs file:

public Startup(IHostingEnvironment env, IApplicationEnvironment app)
{
    Configuration = new ConfigurationBuilder(app.ApplicationBasePath)
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables()
        .Build();

    Configuration.Set("ASPNET_ENV", "Development");
}

My config.json file is currently an empty object { }. How do I add variables to this file (syntax?), and how do I access them from code?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
  • Have you read through http://docs.asp.net/en/latest/fundamentals/environments.html? You've already got code to load an environment-specific JSON file - do you *have* such files? – Jon Skeet Aug 19 '15 at 08:49

2 Answers2

1

Note this line in your startup code:

.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true)

This adds an additional JSON config file which is named depending on your environment name. So add a new file to your project called:

config.<environment-name>.json

And set up the details in there, such as:

{
  "AppSettings": {
    "SomeVariable": "Blah"
  },
  "Data": {
    "YourConnectionString": {
      "ConnectionString": "<connection string here>"
    }
  }
}

For reading the configuration, there's a good answer here: Using IConfiguration globally in mvc6

Community
  • 1
  • 1
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Thanks DavidG. I followed the example in the answer you linked to, but it seems overly cumbersome - is it really necessary to have an "options" class and add every single configuration key to my ConfigureServices method in my startup class? Can't I just read directly from the file, or is this bad practice? – 08Dc91wk Aug 19 '15 at 10:27
  • There is a lot more code in the answer I linked but overall it's a pretty way of doing things. Once you get your head round DI, it's actually pretty simple. – DavidG Aug 19 '15 at 10:40
  • As for being bad practice, it's not really bad to read from the file directly, but what happens if in future you decide to move the config to a database for example? Using this method it's a simple change of one file but if you read directly from the file all over your code, you will have many more changes to make and test. – DavidG Aug 19 '15 at 10:44
  • Thanks @DavidG, I see the error of my ways. It's working perfectly. – 08Dc91wk Aug 19 '15 at 11:27
0

You can use plain old JSON for that. For example, here's how to define a connection string:

{
  "Foo": "Bar"
}

To access the configuration value, you do:

config.Get("username")

or:

config["username"];

You can find more information here: http://docs.asp.net/en/latest/fundamentals/configuration.html

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • 1
    Triple backticks don't work in Stack Exchange markdown - just indent the block of code (or JSON, or whatever) to format it. – Jon Skeet Aug 19 '15 at 08:50
  • 1
    Thanks Erik, but I don't understand how you got the `config` variable? Also, I take it "username" should be "Foo" in order to get the string "Bar"? – 08Dc91wk Aug 19 '15 at 10:16