Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 489
bastos.sergio Avatar asked Nov 10 '11 12:11

bastos.sergio


People also ask

How add config transform?

If you have a web application project, Right-click on web. config and choose Add Config Transform. This will add any config transforms that are missing from your project based on build configurations (i.e. if you have Production and Staging build configs, both will get a transform added).

What is a config transform?

config transformation file contains XML markup that specifies how to change the Web. config file when it is deployed. You can specify different changes for specific build configurations and for specific publish profiles.

How do I use different Web config files?

config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release. If you want to transform other files you can do that too.


2 Answers

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

like image 111
Mrchief Avatar answered Oct 08 '22 12:10

Mrchief


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.

like image 45
PhilPursglove Avatar answered Oct 08 '22 13:10

PhilPursglove