Malcolm
Malcolm

Reputation: 12864

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.

If you have different config for each type when you publish a ASP.NET website doesn't the config get copied as well??

Malcolm

Upvotes: 4

Views: 157

Answers (3)

Amitabh
Amitabh

Reputation: 61177

In Visual Studio 2010 you can maintain Multiple Web.Config and use a transformation to generate the correct Configuration for an environment.

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 e.g.

Web.Debug.Config Web.Staging.Config Web.Production.Config

The Transformation file can override the value of a particular config item for the environment e.g.

<?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.

Upvotes: 1

No Refunds No Returns
No Refunds No Returns

Reputation: 8336

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. Source control has dev/test/prod/ etc. web.config files.

Upvotes: 3

Related Questions