Multiple web.config Files for each Build Configuration

This is a neat trick that you can use if you're not using Visual Studio 2010 yet. It allows you to have multiple web.config files for each type of build configuration that your project has. No longer do you have to do any sort of copy/paste. Plus, if you use something like MSBuild in CruiseControl.net, it will work there too (if you're building from the sln file)!

You will need to create two additional web.config files. I name them like this web.buildConfiguration.config where buildConfiguration is Debug, Release, etc. Each of your new config files will contain the settings appropriate for the build configuration you are targeting.

Open your project properties and select Build Events. Under the 'Pre-build event command line' paste this in :

if $(ConfigurationName) == Release copy $(ProjectDir)Web.release.config $(ProjectDir)Web.config
if $(ConfigurationName) == Debug copy $(ProjectDir)Web.debug.config $(ProjectDir)Web.config

when you build, your web.config will be replaced with the correct one for _that_ build configuration.

About Brian


profile for bluevoodoo1 on Stack Exchange, a network of free, community-driven Q&A sites

Brian Canzanella brings you nifty tips and tricks for most things .NET. read more...

Readers / Stuff