How to use one appsettings.json file across muiltiple applications



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

Solution: 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>()
}





Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback

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

Your Session is Ending
Login to Continue


© 2021 - ErnesTech - Privacy