Visual Studio 2010: Web.config transforms

Recently I wrote about Visual Studio 2010 and multiple web.config files support. Let’s see now how to play with web.config transformation so we can use one configuration on development environment and the other for production environment.

Adding config transformations

add-web-config-transformWhen we create web application project the web.config file is created automatically. web.config contains some section definitions and assembly, caching, pages, handlers and other configuration settings.

Besides them we can add our own settings to appSettings block and connection strings to connectionStrings block. These two blocks are the main sources of differences between development, test and production environments. appSettings may include settings for paths that are different in different environments. I think there is no need to mention connectionStrings.

Web.config versions are bound to solution configurations. By default, solutions have two configurations: Debug and Release. If you want you can define more configurations. By example you may want to add special configuration for test environment.

Adding config transforms is simple. Just right click on Web.config file and select Add Config Transforms. Visual Studio 2010 will add transforms automatically. You can read in this point my blog entry Visual Studio 2010: Multiple web.config versions.

Understanding transformations

Web.config transforms are not separate versions of main Web.config file. Web.config file that was created by default contains all configuration options. Transform files contain only transforms – that is, modifications that one or another configuration needs in Web.config file to be made.

You can find more information about transforms from Visual Web Developer Team Blogposting Web Deployment: Web.Config Transformation.

Changing connection string for release configuration

By default, new transform for release configuration looks like this.

<?xml version="1.0"?>
 
<!-- For more information on using web.config transformation
     visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

 
<configuration
 
 
xmlns:xdt=http://schemas.microsoft.com/XML-Document-Transform>
 
 
<system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
 
</configuration
>

As we can see there is only one transformation rule defined – remove debug attribute. We can also change values of attributes. Let’s add new connection string to Web.Config file.

<connectionStrings>
  <add name="MyGallery"
 
   
connectionString="Default connection string"
 
   
providerName="System.Data.SqlClient" 
/>
</
connectionStrings
>

This is default connection string that is used by all configurations that has no transform for this connection string. We wanted to use different connection string for product system where we use release configuration. Let’s add the following block to Web.Release.Config file.

<connectionStrings>
  <add
      name="MyGallery"
      connectionString="Production connection string"
      providerName="System.Data.SqlClient"
      xdt:Transform="Replace" xdt:Locator="Match(name)"
   
/>
</
connectionStrings
>

Basically the connection string is like usual connection string in Web.config file. But there are two new attributes: Transform and Locator. These attributes tell to Visual Studio 2010 packaging system how to modify Web.config so it works with current solution configuration.

  • Transform. Tells to Visual Studio that we want to replace original connection string.
  • Locator. Tells to Visual Studio how to find the configuration option that needs transformation.

There are many other transforms you can use to make modifications to Web.config. You can find more information about transforms from Visual Web Developer Team Blog posting Web Deployment: Web.Config Transformation.

To get transformed Web.config file you have to create package of you application.

Visual Studio: Create package

Deployment packages is one of the new and cool features of Visual Studio 2010. I will write about them a little bit later this week.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    One thought on “Visual Studio 2010: Web.config transforms

    Leave a Reply

    Your email address will not be published. Required fields are marked *