Tip/Trick: Automating Dev, QA, Staging, and Production Web.Config Settings with VS 2005
 
Published: 21 Sep 2007
Unedited - Community Contributed
Abstract
This article examines how to automate some of the Web.Config settings using Visual Studio 2005.
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 19833/ 47

Introduction

Republished with Permission - Original Article

One of the questions I get asked fairly regularly is: "how can I can easily change different configuration settings in my web.config file based on whether my application is in a dev, qa, staging or production mode?"  The most common scenario for this is one where an application uses different database connection-strings for testing and production purposes.

It turns out you can easily automate this configuration process within the Visual Studio build environment (and do so in a way that works both within the IDE, as well as with command-line/automated builds).  Below are the high-level steps you take to do this.  They work with both VS 2005 and VS 2008.

Use ASP.NET Web Application Projects (which have MSBuild based project files)

Open the VS Configuration Manager and create new "Dev", "QA", "Staging" build configurations for your project and solution

Add new "web.config.dev", "web.config.qa", and "web.config.staging" files in your project and customize them to contain the app's mode specific configuration settings

Add a new "pre-build event" command to your project file that can automatically copy over the web.config file in your project with the appropriate mode specific version each time you build the project (for example: if your solution was in the "Dev" configuration, it would copy the web.config.dev settings to the main web.config file).

Once you follow these steps, you can then just pick the mode your solution is in using the configuration drop-down in the VS standard toolbar:

Figure 1

The next time you build/run after changing the configuration mode, VS will automatically modify your application's web.config file to pick up and use the web.config settings specific to that build configuration (so if you select QA it will use the QA settings, if you select Deploy it will use the Deploy settings).

The benefit with this approach is that it works well in a source control environment (everyone can sync and build locally without having to make any manual changes on their local machines).  It also works on a build server - including with scenarios where you are doing automated command-line solution builds.

Summary

To learn more about the exact steps to set this up, please read the Managing Multiple Configuration File Environments with Pre-Build Events post that Scott Hanselman published earlier tonight.  Also check out my ASP.NET Tips, Tricks, and Gotchas page for other ASP.NET Tips/Tricks recommendations.

Hope this helps,

Scott

Resources



User Comments

Title: Thanks! Very Helpful   
Name: Stephanie
Date: 5/29/2008 11:30:11 AM
Comment:
Thanks! I am going to first try this on my new project at work then I may integrate it into my own personal websites.
Thanks again for the help!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 11/1/2014 2:46:13 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search