19

In 'the old days' using XML configuration it was possible to include partial configuration from another file like this:

<appSettings file="relative file name">
    <!-- Remaining configuration settings -->
</appSettings>

Now in ASP.NET Core I would like to share some configuration in appsettings.Development.json as well as for example appsettings.Staging.json. Is something like the <appSettings file=""> did also available using the json configuration?

Kapé
  • 4,411
  • 3
  • 37
  • 54
  • 5
    You can add as many JSON files as you like in your startup code. – DavidG Sep 05 '17 at 15:40
  • Of course, but I'm trying to prevent duplication between json files themselves – Kapé Sep 05 '17 at 19:36
  • 4
    It's not clear what you mean by "prevent duplication". You don't need to have duplicate content in additional files, you just add new bits as required and the framework takes care of merging them into a larger config. Each additional file will override the values in a previous one if they were present, or will just add to it if not. – DavidG Sep 06 '17 at 10:56
  • The Q is clear to me. web.config files i.e. allow you to external ref other config files [link](https://www.c-sharpcorner.com/blogs/add-reference-external-config-files-in-webconfig1) Now, I have a host prj and a mirgrate prj and I have to maintain both. – Erik Mar 28 '18 at 12:20

2 Answers2

16

You can add mutliple configuration file in the Startup class:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile("appsettings-Development.json", optional: false)
            .AddJsonFile("appsettings-Staging.json", optional: false)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

See here for more details: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration ...

anserk
  • 1,300
  • 1
  • 9
  • 16
  • 9
    Sorry, but that's not my question. I need for example to include `appsettings-shared.json` into both `appsettings-Development.json` and `appsettings-Staging.json` – Kapé Sep 05 '17 at 19:39
  • 4
    Can you keep those shared settings in a separate file and load it in your startup ? This way you don't have to include those settings in the others because you have them available all the time. – anserk Sep 05 '17 at 19:42
0

If you wish to add a file outside of the project solution folder ie "another file" I made it work by setting .SetBasePath("to whatever path where the settings file is") like so:

any-class-name.cs file:

using Microsoft.Extensions.Configuration;
namespace your-name
{
    static class ConfigurationManager
    {
        public static IConfiguration AppSetting { get; }
        static ConfigurationManager()
        {
            AppSetting = new ConfigurationBuilder()
                    .SetBasePath("C:/Users/Name/Documents/AnotherSettings/") //instead of .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .Build();
        }
    }
}

Hope this helps.

Dmitri K
  • 634
  • 6
  • 13