dotnet publish不会发布正确的应用程序设置。{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版本
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:
- 始终,
- PreserveNewest
- 从不
因此将下一部分添加到您的 .csproj
:
So add next section into your .csproj
:
<ItemGroup>
<None Include="appsettings.Production.json" CopyToPublishDirectory="Always" />
</ItemGroup>
查看 @nover答案和
Look into @nover answer and SO Exclude or include files on publish for more information about file's control during publishing.
在您的<$中,排除或包含发布中的文件。 c $ c> project.json 文件中,您有 publishOptions
部分,以及 include
子节,您已经有一些文件,例如 appsettings.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
放入此数组。
基于评论的更新:
-
请记住,所有
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和环境变量中都存在相同的设置,则环境变量中的设置将被使用。如果一个设置存在于多个位置,则最后一个配置源指定为 wins。 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不会发布正确的应用程序设置。{env.EnvironmentName} .json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!