Web.config Transform variables
Asked Answered
L

2

7

Is it possible to include variables in a web.config transform file? For each environment, I have basically the same transform, just with different values. For example, for the development environment, I'd have...

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

And for the QA environment, I'd have...

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

The only difference is the value for server1 vs server2. This is a simple example, and in reality, I use the server value multiple times in the transform. Is there any way to declare a variable in the transform file to be used multiple times? Something like...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <property name="server" value="server2" />
    <appSettings>
        <add key="serverName" value="${server}" xdt:Transform="Replace" xdt:Locator="Match(key)" />
        <add key="serverPath" value="\\${server}" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>
Lafreniere answered 7/3, 2011 at 21:21 Comment(0)
C
0

I did something similar to this a few months ago. Example is at http://www.geoffhudik.com/tech/2010/10/19/webconfig-automation-with-t4-and-a-macro.html

Chon answered 10/3, 2011 at 5:21 Comment(1)
This is a link-only answer to your own blog. Please republish the relevant material here so that in the event your blog ever goes offline, your answer remains useful to other viewers.Giant
N
4

this is not supported with web.config transformations. One thing that you could take a look at is creating a T4 Template which can be used to generate your web.config transformations. So the idea being that you place the variables in the T4 template, and it will spit out the web.debug.config/web.release.config/etc. Then when you package/publish it will just pick up the transform file which was produced as the T4 output.

I wouldn't mind helping you with such a thing if you can provide some concrete examples of this being useful.

Network answered 9/3, 2011 at 2:28 Comment(0)
C
0

I did something similar to this a few months ago. Example is at http://www.geoffhudik.com/tech/2010/10/19/webconfig-automation-with-t4-and-a-macro.html

Chon answered 10/3, 2011 at 5:21 Comment(1)
This is a link-only answer to your own blog. Please republish the relevant material here so that in the event your blog ever goes offline, your answer remains useful to other viewers.Giant

© 2022 - 2024 — McMap. All rights reserved.