We have an application that runs in three environments: development, QA, and production. The application accesses an SQL server and several web services. The web.config file has the connection string for the SQL server and the IP addresses of the web services. We would like to be able to have one web.config file that works in all three environments, but somehow picks up the varying data for each environment. Does anyone know of a way to do this?
Asked
Active
Viewed 500 times
2 Answers
1
Look at Web.config Transformation.

Thomas Li
- 3,358
- 18
- 14
-
Thanks for this excellent reference. I had no idea that this capability existed. – dna86 May 27 '11 at 12:30
0
We use the exact same config options as you, and we have created 3 connection strings in our web.config like this:
<connectionStrings>
<add name="Dev" connectionString="Server=localhost;Database=WebDev;User=devo;Pwd=xxxxx;" providerName="System.Data.SqlClient"/>
<add name="Stage" connectionString="Server=localhost;Database=WebStage;User=stago;Pwd=xxxxx;" providerName="System.Data.SqlClient"/>
<add name="Live" connectionString="Server=localhost;Database=WebLive;User=livo;Pwd=xxxxx;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Then we have a static method that bases itself on the url to determine which conn str to use:
public static string ConnStr
{
get
{
if (Config.WebRoot.StartsWith("http://www.")) { return ConfigurationManager.ConnectionStrings["Live"].ToString(); }
else if (Config.WebRoot.StartsWith("http://stage.")) { return ConfigurationManager.ConnectionStrings["Stage"].ToString(); }
else if (Config.WebRoot.StartsWith("http://localhost")) { return ConfigurationManager.ConnectionStrings["Dev"].ToString(); }
else { return null; }
}
}
You may have to adjust the method to work for you.

Nelson Pires
- 321
- 4
- 12