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
Best Answer
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: