Solved

Conditional Web Config File in Asp.nET 2.0

Posted on 2007-03-19
7
1,291 Views
Last Modified: 2013-11-07
Is it possible within the web.config under asp.net 2.0 to have conditional statements within the web.config file.

So that if the file is in dev/qa/prod it will know which settings to use for the various environments?

0
Comment
Question by:JDEE8297
  • 4
  • 2
7 Comments
 
LVL 27

Assisted Solution

by:Sammy
Sammy earned 100 total points
ID: 18752916
Nope thats not possible but you can change the web.config file using code behind.
0
 

Author Comment

by:JDEE8297
ID: 18752984
to change the web.config file using code behind your web user would have to read/write access to the file. Is that good practice to do that to a production app?. Not questioning your solution, just want to make sure I understand what is possible in this scenario.
0
 
LVL 15

Expert Comment

by:crisco96
ID: 18755215
If you want to be able to have pretty much the same web.config accross all applications you could do the following:

<add key="Environment" value="Dev" />

<!-- DEV variables -->
<add key="DevConn" value="Dev Database connection string" />
...

<!-- QA variables -->
<add key="QAConn" value="QA database connection string" />
...

<!-- Prod variables -->
<add key="ProdConn" value="Production database connection string" />
...

So now you could just change Environment to be Dev, QA or Prod and whenever you need to reference the connection string you could use:

ConfigurationSettings.AppSettings[ConfigurationSettings.AppSettings["Environment"] + "Conn"];

Setting up your web.config in this way may be easier for you.


I wouldn't recommend dynamically changing your web.config with code because each time you modify the web.config your application restarts killing everyone's sessions.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:JDEE8297
ID: 18755476
Crisco96

I like your approach, so I was wondering if it would be possible to split it into sections, as in have a new section within the web config and then based on the environment value I would pull from that section from the web config. I seem to remember reading something about custom sections in the web config and was wondering what your thoughts were on it.
0
 
LVL 15

Accepted Solution

by:
crisco96 earned 150 total points
ID: 18755505
It seems like you're thinking about the configSource tag which allows you to specify a different config file to pull appsettings or connectionstrings from.  

You can read more about it here: http://aspalliance.com/820

I don't know if using this would be any better than just having different web.configs for dev, qa or production, it just changes it so they are pulled from another config rather than the base web.config.
0
 

Author Comment

by:JDEE8297
ID: 18755572
true, but the problem is that no matter how much one places in the honour system, someone will always push the wrong config file to the wrong environment and hey presto the site will either go down or not display the correct information.

And the funny thing is that this will be up on the site for a week or so before anyone notices it, funny how that goes...:)
0
 

Author Comment

by:JDEE8297
ID: 18755578
Thank you for help on this one, gave me some ideas on how to set this up.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question