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.
Related videos on Youtube
Author by
Kapé
Updated on September 16, 2022Comments
-
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 exampleappsettings.Staging.json
. Is something like the<appSettings file="">
did also available using the json configuration?-
DavidG over 6 yearsYou can add as many JSON files as you like in your startup code.
-
Kapé over 6 yearsOf course, but I'm trying to prevent duplication between json files themselves
-
DavidG over 6 yearsIt'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 about 6 yearsThe 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é over 6 yearsSorry, but that's not my question. I need for example to include
appsettings-shared.json
into bothappsettings-Development.json
andappsettings-Staging.json
-
anserk over 6 yearsCan 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 almost 2 yearsI fail to understand why, when a question CLEARLY asks for
X
, people answer forY
. This is NOT what the OP asked for !