23

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?

4
  • 5
    You can add as many JSON files as you like in your startup code. Commented Sep 5, 2017 at 15:40
  • Of course, but I'm trying to prevent duplication between json files themselves Commented Sep 5, 2017 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. Commented Sep 6, 2017 at 10:56
  • 1
    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. Commented Mar 28, 2018 at 12:20

3 Answers 3

17

You can add multiple 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 ...

Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
1

It's not possible to directly include one JSON file into another. If you avoid duplication, your best bet is probably to have appsettings.json and appsettings.<env>.json. The final config is basically a sum of these two. If there are duplicates, appsettings.<env>.json has higher priority.

.NET config is not limited to .json files. It uses, among other things, environment vars. So you might try your luck wiht dontenv files

Comments

-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.

2 Comments

I fail to understand why, when a question CLEARLY asks for X, people answer for Y. This is NOT what the OP asked for !
The question is How to use a file like abcd.xyz in config

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.