Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

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

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.

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 Blog posting 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.

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.


kick it on DotNetKicks.com vote it on WebDevVote.com pimp it Progg it Shout it

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# June 15, 2009 7:36 PM

DotNetBurner - ASP.net said:

DotNetBurner - burning hot .net content

# June 15, 2009 7:39 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# June 15, 2009 7:40 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# June 15, 2009 7:43 PM

WebDevVote.com said:

You are voted (great) - Trackback from WebDevVote.com

# June 15, 2009 7:45 PM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# June 15, 2009 7:49 PM

Gunnar Peipman's ASP.NET blog said:

Here are my postings about Visual Studio 2010 and .Net Framework 4.0 that may be interesting to my readers

# June 15, 2009 7:51 PM

Visual Studio 2010: Web.config transforms - Gunnar Peipman's ASP … said:

Pingback from  Visual Studio 2010: Web.config transforms - Gunnar Peipman&#39;s ASP &#8230;

# June 16, 2009 3:56 AM

ASPInsiders said:

If you are looking to follow this series, be sure to subscribe to my RSS feed at feeds.jasongaylord.com/JasonNGaylord

# June 16, 2009 8:10 PM

The Technology Post for June 16th, 2009 | rapid-DEV.net said:

Pingback from  The Technology Post for June 16th, 2009 | rapid-DEV.net

# June 17, 2009 6:57 AM

This week on C9: Get paid for open source, Azure for dating, debugging and naming lore | Tech-monkey.info Blogs said:

Pingback from  This week on C9: Get paid for open source, Azure for dating, debugging and naming lore | Tech-monkey.info Blogs

# June 19, 2009 9:04 PM

ComponentGear.com Feed said:

This week on Channel 9, Brian and Dan discuss the top developer news, including: - Peli de Halleux -

# June 19, 2009 9:33 PM

This week on C9: Get paid for open source, Azure for dating, debugging and naming lore | CHARGED's Digital Lifestyle at Work or Play said:

Pingback from  This week on C9: Get paid for open source, Azure for dating, debugging and naming lore | CHARGED's Digital Lifestyle at Work or Play

# June 22, 2009 4:54 AM

Sanjeev Agarwal said:

Daily tech links for .net and related technologies - June 19-23, 2009 Web Development Rapid Application

# June 23, 2009 1:04 AM

www.onlineisgreat.com said:

Yep i got the problem with the connection string

# October 23, 2009 10:46 AM

ASP.NET Chinese Blogs said:

一般来说,在您将 Web 应用 正式部署于生产环境前,它已经被用于多个环境了。这些环境可能包括: 开发人员开发环境、质量保证 (QA) 以及用户验收测试 (UAT)/暂存/预生产环境。在这些环境中转换应用程序时

# January 10, 2010 9:45 PM

Visual studio 2010 – Per Developer/machine/environment Web.Config settings - Programmers Goodies said:

Pingback from  Visual studio 2010 &#8211; Per Developer/machine/environment Web.Config settings - Programmers Goodies

# September 16, 2011 1:00 AM

Visual studio 2010 – Per Developer/machine/environment Web.Config settings - Programmers Goodies said:

Pingback from  Visual studio 2010 &#8211; Per Developer/machine/environment Web.Config settings - Programmers Goodies

# September 16, 2011 1:00 AM