'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 1:[1]
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.
Solution 2:[2]
If you truly don't want it in your source control, the best advice is to keep it where it is and simply add it to the ignores for your source control, so it's never committed, as @PmanAce suggested.
However, I personally think files like appsettings.json
should be committed. They essentially serve to document the configuration necessary for the application. Without that as a guide, how is any one supposed to know what should go in the appsettings.json
they'll have to create because it won't be included in the pull from your source control?
That said, there is the issue of storing sensitive data to think about. However, there's other ways around this. You can use other means of configuration such as environment variables and/or user secrets to override anything in appsettings.json
. Therefore, for things like connection strings, provide a placeholder in your appsettings.json
so it's self-documenting that a value for that needs to be provided, but for your own personal use, utilize user secrets and/or environment variables to override that value with something real. That way, you can freely and safely commit appsettings.json
, fully documenting what settings are necessary for your application, without worry that personal or secret information is being included with it.
Solution 3:[3]
For .Net Core 6
i had to set root folder before i could change app configuration as posted by @ian-warburton, because if not it throws the error:
System.NotSupportedException: The web root changed from "C:\app\wwwroot" to "C:\app". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.
The folder were the app will run is the current working directory. It is valid for running from visual studio and docker container with the standard Dockerfile, at least.
const string ConfigFolder = "./Config"; //can be relative or fullpath
IConfiguration configuration = null; //so it can be used on other configuration functions bellow
//create the WebApplication on the current folder
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args =args,
WebRootPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "wwwroot")), //point where if serving static files from default location. Works for my case. May need some adjustments in other cases.
});
//load config file, build it, give it to WebApplication and keep the config variable for further use.
builder.WebHost.ConfigureAppConfiguration(
(hostingContext, config) =>
{
var path = Path.Combine( ConfigFolder, "appsettings.json");
config.AddJsonFile(path, optional: false, reloadOnChange: true);
config.AddEnvironmentVariables();
configuration = config.Build();
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | JLRishe |
Solution 2 | Chris Pratt |
Solution 3 |