How to use one appsettings.json file across muiltiple applications

Question: How do you use one appsettings.json file across muiltiple applications


Please Login to see the rest of the answer

Answer: The other way you could set it so that every project uses the same settings is through the IWebHostBuilder in the Program.cs.

1. Create an appsettings.json file in the location you want
2. Navigate to Program.cs file, under the IWebHostBuilder function type:

public static IWebHostBuilder CreateDefaultBuilder(args){
return WebHost.CreateDefaultBuilder(args)
.UseKestrel() //If you intend to use Kestrel Web Server
.ConfigureLogging((hostingContext, logging)=>{
//In here that is where you include the global appsettings.json file

var pathToJsonFileLocation = Path.Combine(env.ContentRootPath,"LocationToJsonFileFolder","JsonFileName")
config.AddJsonFile(pathToJsonFileLocation, optional: true)
.AddJsonFile("AnotherAppsettingsIfYouWant.json", optional: true)
.AddJsonFile($"AnotherNeatWayOfGettingProductionJsonFile.{env.EnviromentName}.json", optional: true);

config.AddEnviromentVariables();
})
.UseStartup<Startup>()
}






Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy