Click here to Skip to main content
15,891,828 members
Articles / Web Development / ASP.NET
Article

One web.config for all developers

Rate me:
Please Sign up or sign in to vote.
2.88/5 (11 votes)
5 Aug 20041 min read 67.1K   17   10
Allows ASP.NET developers and the final production environment to share one web.config file

Image 1

Introduction

Working on a multi-developer ASP.NET project while sharing one web.config can be time consuming. Usually, each developer may have path and other settings specific to their development machine, such as database connection strings, paths, e-mail addresses, etc.

Background

Switching from a ASP 3.0 server-based development environment to a code versioning ASP.NET local-based development environment posed the challenge of all development machines having different settings. It is too time-consuming and tedious to have multiple copies of web.config, which is why we came up with the notion of reading the machine name and loading the appropriate settings from there.

Using the code

One way to determine and load the appropriate web.config setting is by first reading the machine (computer) name (Note: Ensure System.Environment is not locked down in the ISP’s hosting policy). Once the machine name is obtained, it can then be used as a numeric offset that is used in all other web.config variables that would be developer-specific. A good place to read these settings may be in Global.asax’s Session_Start() method.

C#
// Get this machine's name.
thisMachineName = System.Environment.MachineName.ToUpper();

// Get production machine name from web.config.
machineNameSetting = ConfigurationSettings.AppSettings["machine_name_remote"];

// Was the production machine found?
if (thisMachineName == machineNameSetting)
{
    // Load "remote/live/production" settings.
    index = "_remote";
    devMachineFound = true;
}
else
{
    // Load developer settings...
    // (see demo project)
}

Points of Interest

While this code works well, the ability to retrieve the machine name may be locked down by your host's security policy. Please check with them before assuming this code will run.

History

This methodology is used in all of our ASP.NET applications.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat for use with a CVS Pin
PaKettle10-Aug-04 4:00
PaKettle10-Aug-04 4:00 
GeneralRe: Great for use with a CVS Pin
Jason Noftall10-Aug-04 4:08
Jason Noftall10-Aug-04 4:08 
GeneralRe: Great for use with a CVS Pin
Ashley van Gerven30-Aug-04 3:44
Ashley van Gerven30-Aug-04 3:44 
GeneralStandard override possibility for web.config/appSettings Pin
Victor Vogelpoel10-Aug-04 1:53
Victor Vogelpoel10-Aug-04 1:53 
Generalgood, but could be explained a bit better Pin
Ashley van Gerven7-Aug-04 1:20
Ashley van Gerven7-Aug-04 1:20 
GeneralRe: good, but could be explained a bit better Pin
Jason Noftall9-Aug-04 14:27
Jason Noftall9-Aug-04 14:27 
GeneralRe: good, but could be explained a bit better Pin
Ashley van Gerven9-Aug-04 18:12
Ashley van Gerven9-Aug-04 18:12 
GeneralI'm missing something. Pin
soup6-Aug-04 5:13
soup6-Aug-04 5:13 
GeneralRe: I'm missing something. Pin
Ashley van Gerven7-Aug-04 1:24
Ashley van Gerven7-Aug-04 1:24 
GeneralRe: I'm missing something. Pin
Jason Noftall9-Aug-04 14:14
Jason Noftall9-Aug-04 14:14 
Hi Simon,
Maybe the article title is a little misleading. You are correct in that every developer does have a copy of web.config but because of each unique machine name and the corresponding index associated with each name, everybody can have their own "db connection string", "send error email to", "localhost/url" paths, etc.

Hope this helps...

Cheers,
Jason

soup wrote:
Hi,

Thanks for sharing your ideas.

However, I am not sure the benefit of your solution proposed here. We have multiple developers on our project and inevitably suffer from each developer having their own we.config file as their machine is their machine. This means, as you identify, that paths differ, and sometimes database locations.

Doesn't using your solution mean all developers have one web.config, but the problem is deferred to each developer requiring some other store for their individual settings?

If this is the case, then each developer still has their own file, it just isn't the web.config file.

Sorry, I don't want to trample on you, I'm just thinking I might not have gotten the big picture.

Cheers,

Simon

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.