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
and Production
( Like shown in picture below). and then 3 .pubxml
files, one for each configuration. Do i need to use the same approach for ASP.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
In Visual Studio, we can set ASPNETCORE_ENVIRONMENT in the debug tab of project properties. Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below.
In Visual Studio 2019 right-click your project, choose Properties . In the project properties window, select the Debug tab. Then, under Environment variables change the value of your environment from Development to Production or other environments.
If you need to check whether the application is running in a particular environment, use env. IsEnvironment("environmentname") since it will correctly ignore case (instead of checking if env. EnvironmentName == "Development" for example).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With