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:
- Always,
- PreserveNewest
- Never
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:
Keep in mind, that all the appsettings.*.json
files like appsettings.development.json
, appsettings.staging.json
and appsettings.production.json
will always end up in all environments. You cannot simply handle this using project.json
, as it does not support any condition rules. This will be changed in future, when project.json
will be replaced back to msbuild
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:
The order in which configuration sources are specified is important, as this establishes the precedence with which settings will be applied if they exist in multiple locations. In the example below, if the same setting exists in both appsettings.json and in an environment variable, the setting from the environment variable will be the one that is used. The last configuration source specified “wins” if a setting exists in more than one location. The ASP.NET team recommends specifying environment variables last, so that the local environment can override anything set in deployed configuration files.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…