I need to change my connection string in the web.config file based on an environment variable (for different enviornments, like dev/staging/production, etc). I have seen other solutions that use build tasks to accomplish changing different configurations, but haven't been able to find something that will let me change my connection string based on an environment variable. Does anyone know of any way to do this?
我需要根据环境变量更改web.config文件中的连接字符串(对于不同的环境,如dev / staging / production等)。我已经看到其他使用构建任务来完成更改不同配置的解决方案,但是却找不到能让我根据环境变量更改连接字符串的东西。有谁知道有任何方法可以做到这一点?
4 个解决方案
#1
8
We make use of the configSource attribute for the appSettings and connectionStrings elements in the web.config.
我们在web.config中使用appSettings和connectionStrings元素的configSource属性。
Basically, we have the same web.config file for all of our environments: dev, qa and production.
基本上,我们为所有环境都有相同的web.config文件:dev,qa和production。
Then we utilize seperate "environment specific" files.. For example...
然后我们使用单独的“环境特定”文件..例如......
In web.config:
在web.config中:
<?xml version="1.0"?>
<configuration>
<appSettings configSource="local.appsettings.config" />
<connectionStrings configSource="local.connectionstrings.config" />
</configuration>
Then we maintain the following files:
然后我们维护以下文件:
local.appsettings.config.development
local.appsettings.config.qa
local.appsettings.config.production
local.connectionstrings.config.development
local.connectionstrings.config.qa
local.connectionstrings.config.production
Since we pre-compile all of our asp.net applications before deployment, we've got a custom msBuild task utilized by our CI solution that copies the right configuration files (based on the target environment) to the proper .config file...
由于我们在部署之前预编译了所有的asp.net应用程序,因此我们的CI解决方案使用了自定义的msBuild任务,将正确的配置文件(基于目标环境)复制到正确的.config文件中...
So, if we are deploying to dev, local.appsettings.config.development -> local.appsettings.config
所以,如果我们要部署到dev,local.appsettings.config.development - > local.appsettings.config
If we are deploying to qa, local.appsettings.config.qa -> local.appsettings.config
如果我们要部署到qa,local.appsettings.config.qa - > local.appsettings.config
This allows us to keep the core web.config the same across all of our environments.
这使我们能够在所有环境中保持核心web.config相同。
#2
2
How about having two connection strings and another variable, like "isTesting
" in your web.config, then based on the value of isTesting
pick which connection string to use?
如何在web.config中使用两个连接字符串和另一个变量,如“isTesting”,然后根据isTesting的值选择要使用的连接字符串?
#3
0
you can also use config sections, and based upon server name switch between sections. this way you can have keys named the same.
您还可以使用配置节,并根据节之间的服务器名称切换。这样你就可以拥有相同的键。
链接文字
#4
0
You can set a web.config for each environment in the configuration manager using prebuild events. I have tried this with excellent results.
您可以使用预构建事件为配置管理器中的每个环境设置web.config。我试过这个效果非常好。
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
When you have debug and build you can have local/preproduction/production... etc
当您进行调试和构建时,您可以进行本地/预生产/生产......等
#1
8
We make use of the configSource attribute for the appSettings and connectionStrings elements in the web.config.
我们在web.config中使用appSettings和connectionStrings元素的configSource属性。
Basically, we have the same web.config file for all of our environments: dev, qa and production.
基本上,我们为所有环境都有相同的web.config文件:dev,qa和production。
Then we utilize seperate "environment specific" files.. For example...
然后我们使用单独的“环境特定”文件..例如......
In web.config:
在web.config中:
<?xml version="1.0"?>
<configuration>
<appSettings configSource="local.appsettings.config" />
<connectionStrings configSource="local.connectionstrings.config" />
</configuration>
Then we maintain the following files:
然后我们维护以下文件:
local.appsettings.config.development
local.appsettings.config.qa
local.appsettings.config.production
local.connectionstrings.config.development
local.connectionstrings.config.qa
local.connectionstrings.config.production
Since we pre-compile all of our asp.net applications before deployment, we've got a custom msBuild task utilized by our CI solution that copies the right configuration files (based on the target environment) to the proper .config file...
由于我们在部署之前预编译了所有的asp.net应用程序,因此我们的CI解决方案使用了自定义的msBuild任务,将正确的配置文件(基于目标环境)复制到正确的.config文件中...
So, if we are deploying to dev, local.appsettings.config.development -> local.appsettings.config
所以,如果我们要部署到dev,local.appsettings.config.development - > local.appsettings.config
If we are deploying to qa, local.appsettings.config.qa -> local.appsettings.config
如果我们要部署到qa,local.appsettings.config.qa - > local.appsettings.config
This allows us to keep the core web.config the same across all of our environments.
这使我们能够在所有环境中保持核心web.config相同。
#2
2
How about having two connection strings and another variable, like "isTesting
" in your web.config, then based on the value of isTesting
pick which connection string to use?
如何在web.config中使用两个连接字符串和另一个变量,如“isTesting”,然后根据isTesting的值选择要使用的连接字符串?
#3
0
you can also use config sections, and based upon server name switch between sections. this way you can have keys named the same.
您还可以使用配置节,并根据节之间的服务器名称切换。这样你就可以拥有相同的键。
链接文字
#4
0
You can set a web.config for each environment in the configuration manager using prebuild events. I have tried this with excellent results.
您可以使用预构建事件为配置管理器中的每个环境设置web.config。我试过这个效果非常好。
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
When you have debug and build you can have local/preproduction/production... etc
当您进行调试和构建时,您可以进行本地/预生产/生产......等