在运行时将值替换为web.config

| 我们有一个可以在三种环境中运行的应用程序:开发,质量检查和生产。该应用程序访问一个SQL Server和几个Web服务。 web.config文件具有SQL Server的连接字符串和Web服务的IP地址。我们希望能够有一个在所有三种环境中都能使用的web.config文件,但是以某种方式为每种环境选择不同的数据。有人知道这样做的方法吗?     
已邀请:
查看Web.config转换。     
我们使用与您完全相同的配置选项,并在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; }
    }
}
您可能需要调整方法以适合您。     

要回复问题请先登录注册