Coding Tips & Tricks


Home > Deployment

Web.config Transformation Features in Visual Studio 2010

When developing applications in Visual Studio, I normally pay a lot of attention to my Solution Explorer. However, I recently spotted something that I didn't notice before. Well, at least something that I haven't paid attention to! In Visual Studio 2010, if you click on the Web.config file and expand the node - there are two extra files - Web.debug.config and Web.Release.config. Now if you open them you will notice that they contain XML that is similar to your normal Web.config file.

What do these transformations do? Well, after some investigation I learnt that if you update the Web.Release.config file with the appropriate configurations for your live environment, and change your solution configuration to "Release mode" and publish from your solution explorer, you can override Web.config settings based on environment. If you wanted to update your connection string only when you published in release mode, this is the perfect way to go about it!

Let's run through a simple example. First update your Web.Release.Config file with the appropriate syntax.

Web.Release.config

You should notice the attribute xdt:Transform="Replace" has been added. This tells the build to use this new section in the config file when you publish your project. Next, we can simulate deploying to our live environment. Update your Solution Configuration to "Release mode". As a side note, when you deploy your application to a live environment, you should be publishing in release mode - it will remove the debug="true" attribute from your web.config. Having your application with the debug attribute set to true will cause some considerable reduction in performance. Check out this link for more information.

Release Mode

Then publish the web application via solution explorer. Right click on your application and choose publish. From here you can choose file system or FTP.

Visual Studio Publish

If you now look at your published application, you will notice that there is only one Web.config file. When we open it, the Web.config matches the changes that we made in the Web.Release.config file.

Web.config

Where I work we are currently using TeamCity along with Nant scripts on our CI server. This works great once you are setup in a team environment, but I sometimes feel that working with the Nant script syntax could be easier. I have also worked in an environment before where we used Team Foundation and MSBuild scripts and that worked really well too. However, if you are a lone developer that is working from home or just working on a side project these Web.config transformations are perfect. I know that when I make any small updates to this blog, using the transformations makes life a lot easier.

I have only given you a quick summary on the Web.config transformation syntax that is available for web project deployment. There are also loads of other options that are available to transform your web.config file. You can use Conditions, XPath, Inserts, and Replace. For more information please take a look on the MSDN site.








Comments

Judith - 12/14/2011
Hi, Nice post, i use the web.config transformations for small and home projects. But how to you solve this problem when you are working in a development team of more then one person, and each developer has his own configuration settings needed? At the moment there are several web.configs. Thanks.

Thomas Kuijper - 12/14/2011
This only counts for Web applications not for Websites. Also you can create as many configurations that you want. With that you will be able to make more web.configs. So each developer has his own - Debug Developer 1 Debug Developer 2

Dean Hume - 12/15/2011
@Judith - I would look at something like TeamCity and Nant, it allows you to use different setups for each developer and each environment that you have.


Add your comment

300 Characters left


Please fill this in to confirm that you are human