I have 3 environment specific appsettings
files in my .Net core application
in project.json
I have setup publishOptions
like this. ( based on suggestion here)
"publishOptions": {
"include": [
"wwwroot",
"appsettings.development.json",
"appsettings.staging.json",
"appsettings.production.json",
"web.config"
]
},
I have 3 corresponding startup classes that uses appropriate appsettings
based on environment
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true);
However when I publish the application then all 3 appsettings files end up in all environments. How do I publish environment specific appsetting file?
Best Answer
If someone else is wondering how to use different appsettings for multiple environments here is a possible solution.
dotnet publish --configuration [Debug|Release]
will copy the appropriate appsettings.json file into the publish folder if*.csproj
has a conditional logic for these files:.pubxml
publish profile file (can be found inProperties
->PublishProfiles
of Visual Studio) disable that all content files are included by defaultStartup.cs
try to load both filesI hope this solution, has been helpful.