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?
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
: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 sectionpublishOptions
with subsectioninclude
, where you already have some files like "appsettings.json":You should add
"appsettings.Production.json"
into this array.Updates based on comments:
Keep in mind, that all the
appsettings.*.json
files likeappsettings.development.json
,appsettings.staging.json
andappsettings.production.json
will always end up in all environments. You cannot simply handle this usingproject.json
, as it does not support any condition rules. This will be changed in future, whenproject.json
will be replaced back tomsbuild
and.csproj
. If this is critical for your app, consider to use another configuration store, like Environment Variable, database, etc.Note, that order is important, as determine which settings will be applied if they exist in multiple locations. From documentation:
In your
project.json
there is a sectionpublishOptions
. This lists all the files and folders that will be included when you publish. You will need to update yours to look something like thisYou can also use globbing patterns, so you should find this works too (I haven't tested this one)
After Visual Studio 2017 15.3
Edit the .csproj file to manually exclude files/folder from being published
ref: https://www.danielcrabtree.com/blog/273/fixing-the-duplicate-content-error-after-upgrading-visual-studio-2017
original source
For the new
csproj
project format you have to add a newItemGroup
with the contentIn case you have multiple
appsettings.{env}.json
files simply repeat theContent
tag inside the sameItemGroup
and all your settings files will end up in the publish folder.As mentioned in the comments an even cleaner solution is to use a wildcard include:
And all your
appsettings
files will be published!