Monday, October 12, 2009

Server specific Web.config sections replacement with WDP

In this post I am hoping to explain how you can change your web.config settings per different server environment using Web Deployment Projects (WDP) for VS 2005 or VS 2008…

First of all let me start with the fact that WDP for VS 2005 as well as WDP for VS 2008 are build configuration aware… In fact, special effort was made in WDP 2005 & WDP 2008 to allow configuration specific settings…

That is the reason when you look at the WDP UI you can see configuration specific dropdowns as shown below:

WDP Property Page

You can create one build configuration per your server environment eg. “Testing”, “Staging” , “Production” etc and configure all of your WDP settings per build configuration pretty easily… Learn more how to manage configurations per build environment

Next I want to change connectionStrings section in side the web.config for “Testing” server and “Release” server… The original connection String section in my web.config file looks as below:

<connectionStrings>
<add name="vishal-db" connectionString="Data Source=vijoshi-DevBox; Initial Catalog=vishal-db; Integrated Security=True"/>
</connectionStrings>








  • For “Testing” environment I would like to change my Data Source to be vijoshi-Testing


  • For “Release” environment I would like to change my Data Source to be vijoshi-Release



To accomplish this I am going to add two configuration files to my project named connectionString.Testing.config and connectionString.Release.config… After adding the two files my solution explorer should look as below:



image



The content of connectionString.Testing.config file will look as below:




<connectionStrings>
<add name="vishal-db" connectionString="Data Source=vijoshi-Testing; Initial Catalog=vishal-db; Integrated Security=True"/>
</connectionStrings>

Notice that the connectionString.Testing.config only contains connectionStrings section (as opposed to original web.config which may contain many other sections) … Similar to the above example in connectionString.Release.config file I will only change the Data Source to vijoshi-Release…


After these two files are added I can now open my WDP configuration and go to the “Deployment” node and check the “Enable Web.config file replacement”…  After this the only thing I need to do is write connectionStrings=connectionString.Testing.config  as shown below…


 


connectionStrings=connectionString.Testing.config


 



After this when I build my WDP project the connectionStrings section will be replaced by the content of the connectionString.Testing.config…



Also now I can modify the Configuration from “Testing” to “Release” and set the Web.config file section replacements section to look as below



connectionStrings=connectionString.Release.config 



This way you can also modify the other config sections each in a new line of the text box shown above…



Hope this will help…



-Vishal

2 comments:

Dave said...

i've installed this, how do you create a WDP, it's not in 'add project' in VS2008?

Julio said...

Hi. I have Visual Studio Team System 2008 Developer edition, and when I right click on the web deployment project I dont have displayed the property page that you show in this article (its seems specific to VS2005). I was interested in the web.config replacement, but I cant see how to do it. Thanks