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






Technology
published
v.0.01




For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]