Web && App sub-config files

One of the cool features of VS2010 and web deploy is the ability to have sub-config files. that way you don’t have to comment out (and remember) sections of your web .config for each environment. You get this feature out of the box with web.config files when you create a new web project:

In the master web.config, you pretty much have the same as your normally do:

In the Sub-Config file, you put in the updated value and some special xsd syntax:

When you publish the website, you only have 1 web.config file. What happens is that the sub-config files have their values moved into the master web.config. For example, if you put a Debug connection String in the Web.Debug.Config, that entry is copied into the master web.config.

When you publish with the Solution Configuration set to Debug:

Your Web.Config gets updated:

Note that if you run the website locally inside Visual Studio (Hitting F5), it does not transform because you are not publishing. You need to publish to implement the change.

But what about app.configs? Good question – because this is based on web deploy, you don’t get this feature in an out of the box desktop project. There are two good posts that explain how to update your projects so they support app configs. The most concise is this one. The problem is that it is incomplete, After you follow Phil’s changes to the project file, you also need to follow this posts on how to update you sub-config to use the xdt. For example:

Once you do that, you get the master app.config updated. The other cool thing is that you don’t have to use any kind of publishing – hit F5 and the .config in your target environment folder is crated appropriately.