4

I'm trying to create custom configuration depending on the environment and it seems that the best way is to use config transforms. I'm trying to have the transform happen on build (to test locally), but the changes don't seem to work. Any ideas? Also what is the correct way to have "layered config" for environment on asp.net ?

guiltz_
  • 41
  • 4
  • it should work but just remember to leave debug as default and add an additional transformation for the other environment, so when you use debug you know the defaults apply and when you switch to release and build these will apply but when the application is published, just have a look at your publish location to see if the new transformations applied – Egli Becerra Mar 09 '20 at 23:32
  • @EgliBecerra So the transform should happen during the build? For some reason it doesn't seem to work for me – guiltz_ Mar 10 '20 at 13:34
  • Which version of Visual Studio are you using? and a couple of questions do you know about web.config transformations like the way they are formatted in xml? also have you tried building and publishing to a folder? once you do that check the output of the publish and more specifically the web.config and check if that transform has applied this shouldn't fail. – Egli Becerra Mar 10 '20 at 22:21
  • Just use the 'default' web.config as your development/debugging version, and then the web.release.config would of course continue to be the release version, since its transforms are applied when you publish. – Egli Becerra Mar 10 '20 at 22:33

2 Answers2

0

have a go at this for me...

in the web.config (base) add this configuration under the appSettings node

..
<appSettings>
<!-- Application Settings -->
<add key="IsTest" value="true" />

then in then right click the web.config and add a transformation (Add Config Transform) if you haven't already

and in that config you will only add the transform for that case in particular

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
      <add key="IsTest" value="false" xdt:Transform="Replace" xdt:Locator="Match(key)" />
...

If you are debugging from visual studio your try to keep your "Debug" values in the root config, and then lets say if you add a UAT_Release configuration and a UAT_Release transformation then when publishing your application check that the transforms have applied to your web config (these should be merged back in the base config)

Egli Becerra
  • 961
  • 1
  • 13
  • 25
  • this is for publishing correct? is there a way to do it for debugging? I really appreciate the help – guiltz_ Mar 11 '20 at 13:25
  • yeah this is for publishing, for debugging leave the debug config variables in the base web.config if that makes sense – Egli Becerra Mar 12 '20 at 03:11
0

for none web.config transforms (app.config for example) these do seem to get transformed based on the build configuration

enter image description here

the underlying issue with web configs is that the "original" can not be preserved because the bin for www uses the .config from the project directly. it is not staged as part of the build so applying the transform would perm change the the web config. (there was an attempt extension to solve this for VS but not seeing one for rider just yet)

my solution was to

  1. add a local build profile that was a copy of debug just with a diff name.
  2. crate xdt transforms for all my "develop" settings
  3. right click preview transform and manually apply this transform to web.config (its manual but best i got so far, the perform transform does seem to fail for web.config for me or would us that)
  4. build project using same config as applied to web and all other configs seemed to auto transform for me.
workabyte
  • 3,496
  • 2
  • 27
  • 35