Haseeb Khan
Haseeb Khan

Reputation: 1050

Dotnet Cli - Publish command not changing Environment Variable in web config

I have two asp.net core project which is targeting net5.0. I have web.config in the project as well namely

web.config
web.staging.config
web.production.config

I try to publish the project using the following command

dotnet publish --configuration Staging -o D:\Websites\testwebsite

The above command works perfectly and the environment variable is set in web.config properly.

But when I run the same command in another project it does not change the environment in web.config

Upvotes: 2

Views: 2200

Answers (2)

Haseeb Khan
Haseeb Khan

Reputation: 1050

After struggling I have found a solution.

My main web.config file was missing a location tag while staging and production files have location tags to be replaced. So that's why when I run the following command transformation was not working properly.

dotnet publish --configuration Staging -o D:\Websites\testwebsite

So after removing the location tag form staging and production config transformation was done properly.

Here is an example

web.config

enter image description here

Staging.Config

enter image description here

So in short. This command works properly or transforming the config files

dotnet publish --configuration Staging -o D:\Websites\testwebsite

Upvotes: 0

Tupac
Tupac

Reputation: 2932

Changes made to the web.config directly are lost, when you run publish again.

There are three ways, you can solve this problem.

Modifying the project file (.CsProj) file

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

Adding the EnvironmentName Property in the publish profiles

You will find the publish profile in the folder

Properties/PublishProfiles/<profilename.pubxml>.

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

Command line options using dotnet publish

Pass the property EnvironmentName as a command-line option to the dotnet publish command.

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

All the three methods will update the web.config, when you publish the application.

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

Upvotes: 2

Related Questions