如何在发布文件中设置aspnetcore_environment? [英] How to set aspnetcore_environment in publish file?
问题描述
我有ASP.NET Core应用程序(Web Api)。该文档已经解释了
更新1
我知道我必须设置 ASPNETCORE_ENVIRONMENT
但是我不知道该在哪里设置?在开发过程中,可以在配置文件中的 launchSettings.json
中进行设置,但是问题是,在发布到暂存或生产环境中时如何设置?我们在目标服务器上设置环境变量吗?
更新2
我发现了文章 此处介绍了设置环境变量的不同方法。这部分回答了我的问题。但是,当我发布应用程序时,发布过程在发布 appsettings。{env.EnvironmentName} .json
时不遵循环境变量。针对该问题的单独的帖子
您可以使用以下命令将所需的ASPNETCORE_ENVIRONMENT作为参数传递给dotnet publish命令:
/ p:EnvironmentName =登台
例如
dotnet发布/ p:Configuration =发布/ p:EnvironmentName = Staging
这将生成具有为您的项目指定的正确环境的web.config:
< environmentVariables>
< environmentVariable name = ASPNETCORE_ENVIRONMENT value = Staging />
< / environmentVariables>
I have ASP.NET Core application (Web Api). The documentation has explained working with multiple environments, however it failed to explain how to set aspnetcore_environment
when publishing the web site.
So lets say if i have 3 environments Development
, Staging
and Production
In classic ASP.NET Web Application i used to create 3 build configurations.
Development
,Staging
andProduction
( Like shown in picture below). and then 3.pubxml
files, one for each configuration. Do i need to use the same approach forASP.NET Core
application as well?How do i set
aspnetcore_environment
in.pubxml
file?If the approach specified in Question 1 is obsolete, then what's the alternate approach? ( I use Jenkins for CI)
Update 1
I understand that I have to set ASPNETCORE_ENVIRONMENT
however I am not able to understand where do we set this? During development I can set this in profile in launchSettings.json
, however question was how do we set this when publishing to staging or production? do we set environment variable on the target server itself?
Update 2
I found article here that explains different ways of setting environment variable. This partially answered my question. However when I publish the application, the publish process does not honor the environment variable while publishing appsettings.{env.EnvironmentName}.json
I have created separate post for that question
You could pass in the desired ASPNETCORE_ENVIRONMENT into the dotnet publish command as an argument using:
/p:EnvironmentName=Staging
e.g.
dotnet publish /p:Configuration=Release /p:EnvironmentName=Staging
This will generate out the web.config with the correct environment specified for your project:
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
</environmentVariables>
这篇关于如何在发布文件中设置aspnetcore_environment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!