Aspnetcore: appsettings.json deploying/publishing file environment specific

Created on 12 Sep 2016  路  4Comments  路  Source: dotnet/aspnetcore

Anyone with a solution?
There're two open bounties on stackoverflow for this answer:

How to publish environment specific appsettings in .Net core app?
http://stackoverflow.com/questions/39256057/how-to-publish-environment-specific-appsettings-in-net-core-app

How can I ensure that appsettings.{environment}.json gets copied to the output folder?
http://stackoverflow.com/questions/38178340/how-can-i-ensure-that-appsettings-dev-json-gets-copied-to-the-output-folder

That's sounds simple! Until that gets automated it would required us to copy that manually, or we would need to create our own custom script

All 4 comments

Yeah, we need a way to tell to "dotnet publish" which publish profile (environment) we are publishing. Something like dotnet publish --environment Staging. Also having two configs (appsettings.json & appsettings.Staging.json) in publish folder is a bad idea... It should be merged during publish, like it was with config transform in classic Asp.Net

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

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:

  • First disable that all content files are included by default
<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
</PropertyGroup>
  • Then specify conditional Debug/Release logic
<Choose>
    <When Condition="'$(Configuration)' == 'Debug'">
      <ItemGroup>
        <None Include="appsettings.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
        <None Include="appsettings.prod.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
      </ItemGroup>
    </When>
    <When Condition="'$(Configuration)' == 'Release'">
      <ItemGroup>
        <None Include="appsettings.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
        <None Include="appsettings.prod.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
      </ItemGroup>
    </When>
</Choose>
  • Finally inside Startup.cs try to load both files
public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile($"appsettings.prod.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();

    Configuration = builder.Build();
}

I hope this solution, has been helpful.

When EnableDefaultContentItems is false, this will cause *.cshtml files and other files in wwwroot not able to read and lead to InvalidOperation exception.

Another solution is

  1. Remove element, which will be set to true by default.
  1. Remove appsettings.json as "Content" and include it again with no build action.




  1. Append the section.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kevenvz picture Kevenvz  路  3Comments

UweKeim picture UweKeim  路  3Comments

ermithun picture ermithun  路  3Comments

rynowak picture rynowak  路  3Comments

FourLeafClover picture FourLeafClover  路  3Comments