如何在发布文件中设置 aspnetcore_environment? [英] How to set aspnetcore_environment in publish file?
问题描述
我有 ASP.NET Core 应用程序 (Web Api).文档已经解释了
更新 1
我知道我必须设置 ASPNETCORE_ENVIRONMENT
但是我不明白我们在哪里设置?在开发过程中,我可以在 launchSettings.json
的配置文件中设置它,但是问题是我们如何在发布到暂存或生产时设置它?我们是否在目标服务器上设置环境变量?
更新 2
我发现文章 here 解释了设置环境变量的不同方式.这部分回答了我的问题.但是,当我发布应用程序时,发布过程在发布 appsettings.{env.EnvironmentName}.json
时不遵守环境变量我为这个问题创建了单独的post
您可以使用以下命令将所需的 ASPNETCORE_ENVIRONMENT 作为参数传入 dotnet publish 命令:
/p:EnvironmentName=Staging
例如dotnet publish/p:Configuration=Release/p:EnvironmentName=Staging
这将使用为您的项目指定的正确环境生成 web.config:
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging"/></环境变量>
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屋!