How to set ASPNETCORE_ENVIRONMENT to be considered for publishing an ASP.NET Core application

Other than the options mentioned above, there are a couple of other solutions.

1. Modifying the project file (.CsProj) file

MSBuild supports the EnvironmentName property which can help to set the right environment variable as per the environment you wish to deploy. The environment name would be added in the web.config during the publish phase.

Simply open the project file (*.csProj) and add the following XML.

<!-- Custom property group added to add the environment name during publish

     The EnvironmentName property is used during the publish
     for the environment variable in web.config
-->
<PropertyGroup Condition=" '$(Configuration)' == '' Or '$(Configuration)' == 'Debug'">
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' != '' AND '$(Configuration)' != 'Debug' ">
  <EnvironmentName>Production</EnvironmentName>
</PropertyGroup>

The above code would add the environment name as Development for a debug configuration or if no configuration is specified. For any other configuration, the environment name would be Production in the generated web.config file. More details are here.

2. Adding the EnvironmentName property in the publish profiles.

We can add the <EnvironmentName> property in the publish profile as well. Open the publish profile file which is located at Properties/PublishProfiles/{profilename.pubxml}. This will set the environment name in web.config when the project is published. More details are here.

<PropertyGroup>
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

3. Command line options using dotnet publish

Additionally, we can pass the property EnvironmentName as a command-line option to the dotnet publish command. The following command includes the environment variable as Development in the web.config file.

dotnet publish -c Debug -r win-x64 /p:EnvironmentName=Development


Option1:

To set the ASPNETCORE_ENVIRONMENT environment variable in Windows:

  • Command line - setx ASPNETCORE_ENVIRONMENT "Development"

  • PowerShell - $Env:ASPNETCORE_ENVIRONMENT = "Development"

For other OSes, refer to Use multiple environments in ASP.NET Core

Option 2:

If you want to set ASPNETCORE_ENVIRONMENT using web.config then add aspNetCore like this -

<configuration>
  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\MyApplication.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

A simple way to set it in the Visual Studio IDE.

Menu ProjectPropertiesDebugEnvironment variables

Enter image description here