Click here to Skip to main content
15,881,204 members
Articles / Web Development / ASP.NET

Project Build: Web.config transformation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
22 Aug 2012CPOL1 min read 23.8K   8   1
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
XML
<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
XML
<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
XML
<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)


Written By
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 Pin
Dan Badea16-Apr-13 0:18
Dan Badea16-Apr-13 0:18 
Very useful and to the point.

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

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