Click here to Skip to main content
Click here to Skip to main content

Tagged as

Project Build: Web.config transformation

, 22 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Web.Config Xml Transformation

Introduction

We will learn about: replacing Web.config values using transformation during project build by using VS2010 TransformXml, transforms in debug or release mode from configuration files such as Web.Debug.Config and Web.Release.Config, preview transformation by using tools such as SlowCheetah. You can download SlowCheetah using the Extension Manager from VS2010.

Background

Web.Config transformation syntax is located here, basically here is a simple sample for transformation. 

Web.Debug.Config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ConnectionString" xdt:Locator="Match(key)" xdt:Transform="RemoveAll"/>
    <add key="ConnectionString" 
             value="SERVER=DEBUG_SERVER;UID=TESTUSER;PWD=TESTPASSWORD;" 
             xdt:Transform="Insert"/>
  </appSettings>
</configuration>
Web.Release.Config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ConnectionString" xdt:Locator="Match(key)" xdt:Transform="RemoveAll"/>
    <add key="ConnectionString" 
            value="SERVER=PROD_SERVER;UID=PRODUSER;PWD=PRODPASSWORD;" 
            xdt:Transform="Insert"/>
  </appSettings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)">
    </compilation>
  </system.web>
</configuration>

Using the code

It is preferable to remove all existing values by using xdt:Locator="Match(key)" xdt:Transform="RemoveAll", and then xdt:Transform="Insert", this will ensure any existing values is removed and inserted with the new ones.

Edit the project file using Notepad, add the following tags to the project file. The tags: <UsingTask>, <BeforeCompile>, and <BeforeBuild> is to generate the transform file. The transformation assembly Microsoft.Web.Publishing.Tasks.dll does the actual xml transformation.

During the build process (Ctrl+Shift+B), the ApplyTransform task is executed before compile started, and this will create a output file "Web.Config_output", just before build process started, the output is renamed to "Web.Config", so that this will be the Web.Config used during runtime.

MyProject.csproj
<Project ToolsVersion="4.0" DefaultTargets="Build" 
              xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ...
  <UsingTask TaskName="TransformXml" 
     AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
  <Target Name="ApplyTransform" Condition="Exists('Web.$(Configuration).config')">          
        <TransformXml Source="web.config" 
           Transform="web.$(Configuration).Config" Destination="web.config_output" />
  </Target>
  <Target Name="BeforeCompile">
	<CallTarget Targets="ApplyTransform"/>
  </Target>
  <Target Name="BeforeBuild">
	<Exec Command="attrib -r Web.config"/>
        <Copy SourceFiles="Web.config_output" DestinationFiles="Web.config" /> 
  </Target>
  ...
</Project>

Points of Interest

Not just Web.Config can be transform, just about any XML file can be transformed, just add the TransfromXml element with the source and destination.

More articles about Web.Config transformation, click here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ken C. Len
Software Developer (Senior) LEN Associates Inc.
United States United States
Years of software consulting and software development using Microsoft development products such as Microsoft Content Management System, SQL Server Reporting Service, ASP.Net C# VB.Net, HTML and javascript web development, Visual Studio add-on development, C++ MFC/ATL and COM+ development, and ActiveX components.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberDan Badea16-Apr-13 1:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 22 Aug 2012
Article Copyright 2012 by Ken C. Len
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid