Substituting values into web.config at runtime
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?
Look at Web.config Transformation.
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.
精彩评论