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.
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.
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.
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.
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.
Add your comment