Web.Config Transformation Is Still a Thing

Visual Studio has an old but still helpful way to change your web.config file based on the environment or build mode. This allows you to replace connection strings or the configuration for web services without the need to fiddle with XML. You still need to write XML Transformation code, but that is a lot simpler than modifying everything on your own.

This web.config transformation is one of many supported ways to change your configuration when you deploy with Octopus Deploy. We use it in combination with Windows Authentication Foundation where we only need to switch a few attributes.

You can create a transformation file by right-clicking on your web.config file and then select “Add Config Transform“:

Select Add Config Transform from the context menu

This will create two new files Web.Debug.config and Web.Release.config:

The newly created files

Both files contain a small introduction into the transformation logic. At leas for me this was not enough. You can find a very thorough explanation in the blog post “Web Deployment: Web.Config Transformation” by Vishal R Joshi. His post has many links should you need even more details.

If your transformation logic is complete, you can deploy your application and check if the web.config file was correctly transformed. While this approach works, there is a much faster way as one of my colleagues pointed out. There is a web site called Web.config Transformation Tester that does exactly what the name says. You can enter your web.config file and your transformation logic (PLEASE without your passwords!) and check if the result is what you expect. You can find the code on GitHub and modify it to your needs.

If you need to replace some values in web.config (or app.config) you should look at the possibilities that the configuration transformation offers. It may be all you need and is already available to you inside Visual Studio.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.