简体   繁体   English

从配置访问设置的最佳做法是什么?

[英]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文件。

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.

相关问题 在这种情况下最好的做法是什么? - What's best practice in this situation? 将设置保留在根web.config中是最佳实践 - Is it best practice to keep settings in root web.config ASP.NET - 从XML文件中读取本地化字符串的最佳实践是什么? - ASP.NET - What's the best practice for reading localized strings from XML file? 超出了JavaScriptSerializer.MaxJsonLength。处理此问题的最佳做法是什么? - JavaScriptSerializer.MaxJsonLength exceeded. What's the best practice for handling this? 像Twitter这样进行用户名检查的最佳实践是什么? - What's the best practice for make username check like Twitter? 用户注册的最佳实践或设计模式是什么? - What's the best practice or design pattern for user registration? 在IIS中设置maxAllowedContentLength的最佳做法是什么? - What's the best practice when setting maxAllowedContentLength in IIS? 从访问层到列表项进行一致定义的最佳实践是什么 - What is the best practice to define consistently from access layer to listitems 使用 c# 从 Web.Config 文件访问 SMTP 邮件设置 - Accessing SMTP Mail Settings from Web.Config File by using c# web.config全球化设置中的“ en-US”和“ auto:en-US”有什么区别? - What's the difference between “en-US” and “auto:en-US” in web.config globalization settings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM