VS 2010: Web.Config Transforms extra spaces problem

by Vlad 21. July 2010 20:38

In Visual Studio 2010 you can use xdt transforms to fill the web.config files with the appropriate values for deployment to test or production environments.And it is pretty simple to use:

  • - add Config Transforms to the web.config (like web.Debug.config)
  • - duplicate the sections with the values you want to customize
  • - set the xdt:Locator attribute for the nodes that need to be identified by an attribute (like xdt:Locator="Match(name)")
  • - set the xdt:Transform to control how the values are changed (common uses - xdt:Transform="SetAttributes" to update some attribute values, or xdt:Transform="Replace" to replace the whole node
  • - you get the transformed web.config when publishing or creating a deployment package (or you can create it from command line).

More details on how to use the Web.Config Transformation here.

 

However, there is a nasty bug that you won't probably notice before publishing for the first time.

Let's say that you want to replace a value that is not an attribute, but the inner text of a node.

 <value>Default value here</value>

You can write the transform like this:

 <value xdt:Transform="Replace">Production value</value>

The problem is that in the generated web.config file the value will appear like this:

 <value>Production value
 </value>

The extra spaces (and newline) will be passed to the value and can generate problems if the setting represents a file path for example.

According to Microsoft, this is a known bug and will be fixed in the next Service Pack for Visual Studio.

 

The workaround is to trim the value before using it in code, but that won't help you if the setting belongs to a 3rd party component that you can't modify.

 

Categories: Gotcha | WTF