asp.net-core.net-corepublishdotnet-cli

dotnet publish doesn´t publish correct appsettings.{env.EnvironmentName}.json


When I issue the following command in the command line:

dotnet publish -o "./../output" -c Release

The dotnetcli publishes the project correctly. However, it does not copy the appsettings.Production.json file, only the appsettings.json.

Why is this? I have googled around and read the official core docs, but haven't found how the correct environment appsettings.json is supposed to end up in the publish output.

Should I copy appsettings.Production.json manually to the published folder?


Solution

  • Update: For current (new) .csproj format the CopyToPublishDirectory attribute should be used. It determines whether to copy the file to the publish directory and can have one of the following value:

    So add next section into your .csproj:

    <ItemGroup>
       <None Include="appsettings.Production.json" CopyToPublishDirectory="Always" />
    </ItemGroup>
    

    Look into @nover answer and SO Exclude or include files on publish for more information about file's control during publishing.


    "In your project.json file you have the section publishOptions with subsection include, where you already have some files like "appsettings.json":

    "publishOptions": {
      "include": [
        "appsettings.json",
        "hosting.json",
        "project.json",
        "web.config"
      ]
    },
    

    You should add "appsettings.Production.json" into this array.

    Updates based on comments: