Solved

Conditional Web Config File in Asp.nET 2.0

Posted on 2007-03-19
7
1,304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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