bastos.sergio
bastos.sergio

Reputation: 6764

How to apply config transformations to an external config file

I can't find an example of my question on the web and was wondering if anybody knew a solution. Basically, if on our web.config we point to another file, like so:

<configuration>
  <configSections />
  <appSettings file="AppSettings.config">
</configuration>

then how do we apply transformations to that external file?

Basically, I want to create an AppSettings.config, AppSettings.Debug.config, AppSettings.Release.config and have a transformation run over it... Is this even possible?

Thanks in advance,

Sergio

Upvotes: 32

Views: 10958

Answers (3)

kvarkel
kvarkel

Reputation: 313

Workaround 1 in the accepted answer put me on the right track, but didn't work as is because the transformation isn't quite right. The correct transformation is just

<appSettings file="AppSettings.debug.config" 
         xdt:Transform="SetAttributes"/>

I had to remove xdt:Locator="Match(file)" so that the file attribute itself would change. Web Config Transformations explains that Match(key) will locate the element to change, but will only change the other elements of the node, not the locator/match key itself. There will be only one appSetting per config file, so we don't need to locate a particular instance.

(I don't have enough reputation to comment on the accepted answer, so I posted this as another answer.)

Upvotes: 6

Mrchief
Mrchief

Reputation: 76218

There are few workarounds:

Workaround 1

  • Write AppSettings.Debug.config, AppSettings.Release.config with full values (not with transform attributes)
  • In your web.config, using transformation, substitute with the appropriate file:

web.debug.config

<appSettings file="AppSettings.debug.config" 
             xdt:Transform="SetAttributes" xdt:Locator="Match(file)"/>

web.release.config

<appSettings file="AppSettings.release.config" 
             xdt:Transform="SetAttributes" xdt:Locator="Match(file)"/>

Its less than ideal, kinda defeats the purpose of transforms but may be appropriate based on one's situation than using something like SlowCheetah.

Workaround 2

Use TransformXml build task to transform your files during build as pointed here and here

Upvotes: 33

PhilPursglove
PhilPursglove

Reputation: 12589

There's a Visual Studio plugin project called Slow Cheetah that takes the idea of transformations and allows you to apply it to files other than web.config, I haven't used it but I think it'll do what you want to do. Scott Hanselman did a blog on it.

Upvotes: 9

Related Questions