dotnet publish 不发布正确的 appsettings.{env.EnvironmentName}.json [英] 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
dotnetcli
正确发布项目.但是,它不会复制appsettings.Production.json
文件,只会复制appsettings.json
.
The dotnetcli
publishes the project correctly. However, it does not copy the appsettings.Production.json
file, only the appsettings.json
.
这是为什么?我已经搜索并阅读了官方核心文档,但没有找到正确的环境 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.
我是否应该手动将 appsettings.Production.json
复制到已发布的文件夹中?
Should I copy appsettings.Production.json
manually to the published folder?
推荐答案
更新:对于当前(新).csproj 格式 应该使用 CopyToPublishDirectory
属性.它决定是否将文件复制到发布目录,可以是以下值之一:
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:
- 总是,
- 保留最新
- 从不
因此将下一部分添加到您的 .csproj
中:
So add next section into your .csproj
:
<ItemGroup>
<None Include="appsettings.Production.json" CopyToPublishDirectory="Always" />
</ItemGroup>
查看 @nover 答案 和 SO 在发布时排除或包含文件,了解有关发布期间文件控制的更多信息.
Look into @nover answer and SO Exclude or include files on publish for more information about file's control during publishing.
在您的 project.json
文件中,您有 publishOptions
部分和 include
小节,其中已经有一些文件,例如appsettings.json".json":
"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"
]
},
您应该将 "appsettings.Production.json"
添加到此数组中.
You should add "appsettings.Production.json"
into this array.
根据评论更新:
请记住,所有
appsettings.*.json
文件,例如appsettings.development.json
、appsettings.staging.json
和appsettings.production.json
将始终在所有环境中结束.您不能简单地使用project.json
来处理这个问题,因为它不支持任何条件规则.这将在未来改变,当project.json
将是 替换回 为msbuild
和.csproj
.如果这对您的应用至关重要,请考虑使用其他配置存储,例如环境变量、数据库等.
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:
指定配置源的顺序很重要,因为这确定了设置在多个位置存在时应用的优先级.在下面的示例中,如果 appsettings.json 和环境变量中存在相同的设置,则环境变量中的设置将是使用的设置.如果设置存在于多个位置,则指定的最后一个配置源获胜".ASP.NET 团队建议最后指定环境变量,以便本地环境可以覆盖部署的配置文件中设置的任何内容.
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.
这篇关于dotnet publish 不发布正确的 appsettings.{env.EnvironmentName}.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!