我们有一个在三种环境中运行的应用程序:开发,QA和 生产环境 . 应用程序访问SQL服务器和多个Web服务 . web.config文件具有SQL Server的连接字符串和Web服务的IP地址 . 我们希望能够有一个在所有三种环境中都能运行的web.config文件,但不知何故为每个环境选择不同的数据 . 有谁知道这样做的方法?
看看Web.config Transformation .
我们使用与您完全相同的配置选项,我们在web.config中创建了3个连接字符串,如下所示:
<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>
然后我们有一个静态方法,它基于url来确定使用哪个conn str:
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; } } }
您可能需要调整方法以适合您 .
2 回答
看看Web.config Transformation .
我们使用与您完全相同的配置选项,我们在web.config中创建了3个连接字符串,如下所示:
然后我们有一个静态方法,它基于url来确定使用哪个conn str:
您可能需要调整方法以适合您 .