Config Transform Files for changing Web.config for release (ASP.Net 4.0)

26 Jun 2013 .Net, Visual Studio No Comments

You can change parts of the web.config between different builds (e.g. debug and release). I used it to change whether cookies must only be sent and received over ssl.
It is also useful for changing connection strings from debug to live versions and also user defined keys (examples below).

Clicking the arrow next to Web.config (in the Solution Explorer Visual Studio)  reveals the various transform files. If you have added more build types you can add more transform files by right clicking on Web.config and selecting Add Config Transform.

Examples

Double-click a transform file to open it and see what it is doing. It has some examples, but the syntax I required for the httpcookies element was a different to these.

Remove debug attribute

System.web already contained one transform, which removes the debug attribute from the compilation element within the system.web element.

Web.Release.Config

<system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>

Debug

<system.web>
    <compilation debug="true" targetFramework="4.0">
        ...
    </compilation>
    ...
</system.web>

Release

<system.web>
    <compilation targetFramework="4.0">
        ...
    </compilation>
    ...
</system.web>

Require cookies to be transmitted and requested only over a secure connection

I added another transform which changes the value of a named attribute (requireSSL) in a named element (httpCookies) within the system.web element.

Web.Release.Config

<system.web>
    ...
    <httpCookies requireSSL="true" xdt:Transform="SetAttributes(requireSSL)" />
</system.web>

Debug

<system.web>
    <httpCookies requireSSL="false" />
    ...
</system.web>

Release

<system.web>
    <httpCookies requireSSL="true" />
    ...
</system.web>

Change the value of a defined key

I also added another transform which changes the value attribute of an add element by matching the key attribute (live) within the appSettings element.

Web.Release.Config

<appSettings>
    <add key="live" value="true" xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" />
</appSettings>

Debug

<appSettings>
    ...
    <add key="live" value="false" />
</appSettings>

Release

<appSettings>
    ...
    <add key="live" value="true" />
</appSettings>

Changing a connection string

I did not need this myself but the following¬†changes the connectionString attribute of an add element (within the connectionStrings element) by matching the name attribute (MyDB) and changes the value of the connectionString attribute to ReleaseSQLServer. Note that the attribute name (connectionString) does not need to be specified. You can change multiple attributes this way, by specifying each attribute and its new value (e.g. including providerName=”value” in the add element below).

Web.Release.Config

<connectionStrings>
    <add name="MyDB" connectionString="ReleaseSQLServer" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

Debug

<connectionStrings>
    <add name="MyDB" connectionString="DebugSQLServer" providerName="System.Data.EntityClient" />
</connectionStrings>

Release

<connectionStrings>
    <add name="MyDB" connectionString="ReleaseSQLServer" providerName="System.Data.EntityClient" />
</connectionStrings>

Conclusion

Config transform files seem a very useful tool when using Visual Studio to publish your projects, and not using your own build scripts.

More useful information

Dan Maharry’s explanation of config transform files
MSDN Article on config transform files