Solved

Conditional Web Config File in Asp.nET 2.0

Posted on 2007-03-19
7
1,273 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
Comment Utility
Nope thats not possible but you can change the web.config file using code behind.
0
 

Author Comment

by:JDEE8297
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:JDEE8297
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for help on this one, gave me some ideas on how to set this up.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now