Brian's Blog

items I see across my tribes

Visual Studio 2010: Web.config transforms

June 06
by briancarter 6. June 2010 09:01

Visual Studio 2010 supports multiple web.config files.  Let’s see now how to configure web.config transformation so we can use one configuration on development environment and the other for production environment.

Config transformations

When 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.

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.

Web.config versions are bound to solution configurations. By default, solutions have two configurations: Debug and Release.  You can add more configurations, for example QA.

Understanding transformations

Web.config transforms are not separate versions of the main Web.config file. The Web.config file that was created by default contains all configuration options. Transform files contain only transforms – that is, modifications the configurations require to be unique in Web.config file for that environment.

Changing connection string for release configuration

By default, the new transform for release configuration is sparse.  If you don’t see it under your Web.config – change you VSS to Release and build.

image

As we can see in the release config, there is only one transformation rule defined – remove debug attribute. We can also change values of attributes.

Let’s add a new connection string to Web.Config file.

  <connectionStrings>
    <remove name="MainConnStr" />
    <add name="MainConnStr" connectionString="Data Source=.;Initial Catalog=PondWater3;Integrated Security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

This is the default connection string that is used by all configurations. We wanted to use different connection string for the product system – this is where we use the release configuration.  Review the comments in the Web.Release.config; pretty straight forward.  You can add the prod connection string:

    <add name="MainConnStr"
      connectionString="Data Source=ProdServer;Initial Catalog=PondWater;Integrated Security=true;"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

Basically the connection string is like the 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.   I typically replace the complication section to set debug to false:

    <compilation debug="false" xdt:Transform="Replace">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>

To review the transformed Web.config file, you have to publish your application with the correct target environment set.  Publish with debug and then with release.  Compare the web.config files.  This integrates with MS Build which I will cover shortly.

Categories:


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.