?
Solved

Conditional Web Config File in Asp.nET 2.0

Posted on 2007-03-19
7
Medium Priority
?
1,315 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 Ageil
Sammy Ageil earned 400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 600 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

764 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