Reference another json file in appsettings.json for ASP.NET Core configuration

12,062

Solution 1

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://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration ...

Solution 2

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.

Share:
12,062

Related videos on Youtube

Kapé
Author by

Kapé

Updated on September 16, 2022

Comments

  • Kapé
    Kapé over 1 year

    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?

    • DavidG
      DavidG over 6 years
      You can add as many JSON files as you like in your startup code.
    • Kapé
      Kapé over 6 years
      Of course, but I'm trying to prevent duplication between json files themselves
    • DavidG
      DavidG over 6 years
      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.
    • Erik
      Erik about 6 years
      The Q is clear to me. web.config files i.e. allow you to external ref other config files link Now, I have a host prj and a mirgrate prj and I have to maintain both.
  • Kapé
    Kapé over 6 years
    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
  • anserk
    anserk over 6 years
    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.
  • Ergis
    Ergis almost 2 years
    I fail to understand why, when a question CLEARLY asks for X, people answer for Y. This is NOT what the OP asked for !