ASP.NET Core multiple configurations without using environment variables

This article shows how to use multiple Json configuration files in ASP.NET Core 2.0 which can be used in CI for automatic deployment without requiring environment variables.


2017.08.15: Updated to ASP.NET Core 2.0
2017.07.01: Updated to VS2017 and csproj
2016.07.01: Updated to ASP.NET Core 1.0 RTM
2016.05.17: Updated to ASP.NET Core 1.0 RC2 dotnet
2015.11.18: Updated to ASP.NET Core 1.0 RC1
2015.10.16: Updated to ASP.NET Core 1.0 beta8

In ASP.NET Core, Json, XML and ini configuration files are supported per default. It is possible to create strongly type classes from the configuration files. It is also possible to load different configuration files depending on a run-time variable. An environment variable can be used for this which most of the examples or existing blogs demonstrates. Sometimes it is not possible to use an environment variable due to deployment requirements or restrictions. This example shows how to setup multiple configurations which can be used in automatic deployments for different target systems without using environment variables.

Application Global Configuration

A global application config file is used to set the application staging environment. The values in this file would typically be set from the installer, for example using a WIX silent installer with utils configuration.

     "StagingEnvironment ": "production"

This can then be used in the Startup.cs class in the constructor. The file is loaded and the StagingEnvironment value can now be used.

var globalbuilder = new ConfigurationBuilder()
var globalConfiguration = globalbuilder.Build();

Application Configuration

The application configuration contains all the normal configurations which are required for the application. You can add many sections to the file, or a configuration class per file, or mix it as you require. I usually try to have just one file for all configuration objects when possible.

    "ApplicationConfiguration": {
        "MinimumLevel": 1,
        "AboutMessage": "This is the default configuration"

The configuration section will be loaded into the ApplicationConfiguration class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AspNetCoreConfiguration.Configurations
    public class ApplicationConfiguration
        public int MinimumLevel { get; set; }
        public string AboutMessage { get; set; }

Now the application configuration can be loaded. The stagingEnvironment property loaded from the global configuration is used to load the correct configuration for the required staging target. This is optional, so if nothing is set or found, the default configuration file is used.

string stagingEnvironment = globalConfiguration["StagingEnvironment"];

var builder = new ConfigurationBuilder()
   .AddJsonFile($"config.{stagingEnvironment}.json", optional: true);
Configuration = builder.Build();

The configuration class can then be added the the services. In ASP.NET Core RC2, the package Microsoft.Extensions.Options.ConfigurationExtensions is required.

public void ConfigureServices(IServiceCollection services)

Using the Configuration

Now the configuration can be used. This is added in the constructor of the controller or the class where it is required. You cannot use the class directly, but by using the IOptions. The following example just displays the configuration value in the about HTTP Request method.

using System.Collections.Generic;
using AspNet5Configuration.Configurations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace AspNetCoreConfiguration.Controllers
    public class AboutController : Controller
        private IOptions<ApplicationConfiguration> _optionsApplicationConfiguration;
        public AboutController(IOptions<ApplicationConfiguration> o)
            _optionsApplicationConfiguration = o;

        public IEnumerable<string> Get()
            return new string[] { _optionsApplicationConfiguration.Value.AboutMessage };

Testing the Configurations

The configuration can be viewed by requesting the api/about URL. This shows the used configuration.

Testing with “StagingEnvironment”: “production”


Testing with “StagingEnvironment”: “test”


Testing with “StagingEnvironment”: “unknown”


With this, it is possible to automatically deploy the application without using any environment variables and using different configurations for development, test, production or with different target systems.


Configuration in ASP.NET 5

Introduction to ASP.NET 5, microsoft virtual academy



  1. […] ASP.NET 5 multiple configurations without using environment variables – damienbod takes a look at using different configuration files in your ASP.NET 5 Applications while avoiding the need for environmental variables […]

  2. […] Utiliser la possibilité d’avoir des environnements multiple avec ASP.NET 5. […]

  3. […] ASP.NET Core 1.0 multiple configurations without using environment variables […]

  4. […] Source: ASP.NET Core multiple configurations without using environment variables | Software Engineering […]

  5. Mike Gledhill · · Reply

    Nicely done, but God I hate the way ASP.Net Core relies on that ASPNETCORE_ENVIRONMENT variable. As such, you can’t just swap to a “Production” configuration, and magically have appsettings.Production.json included by default.

    It’s a huge step backwards from the simplicity of regular ASP.Net.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: