Search code examples
asp.net-coreasp.net-core-mvc

Can I move appsettings.json out of the app directory?


I want to put my appsettings.json file outside of the web folder so that I can avoid storing it in source control. How can I do this?


Solution

  • It doesn't like relative paths, it seems. But, this works...

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                if (!hostingContext.HostingEnvironment.IsDevelopment())
                {
                    var parentDir = Directory.GetParent(hostingContext.HostingEnvironment.ContentRootPath);
                    var path = string.Concat(parentDir.FullName, "\\config\\appsettings.json");
    
                    config.AddJsonFile(path, optional: false, reloadOnChange: true);
                }
            })
            .UseStartup<Startup>()
            .Build();
    

    So, it's another settings file on top of the sources added by default.