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