2ooom 2ooom - 4 months ago 14
ASP.NET (C#) Question

How to handle debug/release config transformations in ASP.NET vNext

In previous versions of ASP.NET many of us used

files trasformations that would look something like this:


<add name="AppDB" connectionString="Data Source=(LocalDb)\\..." />


<add name="AppDB" connectionString="Data Source=(ReleaseDb)\\..." xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

As per ASP.NET vNext tutorial you still can use Web.config. But
appear to be the new way to handle configurations now as per the same article:


"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\..."

And in Startup.cs:

var configuration = new Configuration();

So I'm wondering what would be the suggested way to handle config-transofrmation with this shift to json?


vNext uses a new configuration system in which you can read the environment variables through code. So, in this case you would check for the presence of the appropriate environment variable and include the corresponding JSON through code.

For example, you can create a qa.json and prod.json. Set an environment variable, say, "ENV" that points to "qa" and "prod" in those respective environments. Then conditionally you can add the appropriate JSON.

The code could look like this:

1) default.json contains all your default stuff.

2) qa.json and prod.json contain the necessary overrides.

3) Since qa.json and prod.json come later, they will win. If there is a "setting1" in default.json and qa.json, it will automatically pick up the "setting1" in qa.json

 var configuration = new Configuration()

 var envSpecificJson = configuration.Get("ENV") + ".json";