[英]What i s best practice for accessing settings from config?
I want to know what best practice is for accessing settings in config file when you have dev/test/production types. 我想知道当你有dev / test / production类型时访问配置文件中的设置有什么最佳实践。
If you have different config for each type when you publish a ASP.NET website doesn't the config get copied as well?? 如果在发布ASP.NET网站时每种类型都有不同的配置,那么配置也不会被复制?
Malcolm 马尔科姆
We usually manually inject the settings file on each site. 我们通常在每个站点上手动注入设置文件。 I think that it's uncommon, though not unheard of, to actually rely on VS to publish to your production site.
我认为实际依赖VS发布到您的生产网站并不常见,尽管并非闻所未闻。 Source control has dev/test/prod/ etc. web.config files.
源代码控制有dev / test / prod / etc. web.config文件。
ConfigurationManager.AppSettings ? ConfigurationManager.AppSettings?
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
In Visual Studio 2010 you can maintain Multiple Web.Config and use a transformation to generate the correct Configuration for an environment. 在Visual Studio 2010中,您可以维护多个Web.Config并使用转换为环境生成正确的配置。
http://blogs.msdn.com/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx http://blogs.msdn.com/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx
Basically we can make have one default Web.Config and different Transformation files for each environment eg 基本上我们可以为每个环境制作一个默认的Web.Config和不同的Transformation文件,例如
Web.Debug.Config Web.Staging.Config Web.Production.Config Web.Debug.Config Web.Staging.Config Web.Production.Config
The Transformation file can override the value of a particular config item for the environment eg Transformation文件可以覆盖环境的特定配置项的值,例如
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="personalDB"
connectionString="Server=StagingBox; Database=personal; User Id=admin; password=StagingPersonalPassword"
providerName="System.Data.SqlClient" xdt:Transform="Replace" xdt:Locator="Match(name)" />
<add name="professionalDB"
connectionString="Server=StagingBox; Database=professional; User Id=professional; password=StagingProfessionalPassword"
providerName="System.Data.SqlClient" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
Whenever we target build for that environment the Transformation are applied to the default Web.Config file. 每当我们针对该环境构建目标时,Transformation都会应用于默认的Web.Config文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.