Using different Web.config in development and production environment

158,979

Solution 1

In Visual Studio 2010 and above, you now have the ability to apply a transformation to your web.config depending on the build configuration.

When creating a web.config, you can expand the file in the solution explorer, and you will see two files:

  • Web.Debug.Config
  • Web.Release.Config

They contain transformation code that can be used to

  • Change the connection string
  • Remove debugging trace and settings
  • Register error pages

See Web.config Transformation Syntax for Web Application Project Deployment on MSDN for more information.

It is also possible, albeit officially unsupported, to apply the same kind of transformation to an non web application app.config file. See Phil Bolduc blog concerning how to modify your project file to add a new task to msbuild.

This is a long withstanding request on the Visual Studio Uservoice.

An extension for Visual Studio 2010 and above, "SlowCheetah," is available to take care of creating transform for any config file. Starting with Visual Studio 2017.3, SlowCheetah has been integrated into the IDE and the code base is being managed by Microsoft. This new version also support JSON transformation.

Solution 2

The <appSettings> tag in web.config supports a file attribute that will load an external config with it's own set of key/values. These will override any settings you have in your web.config or add to them.

We take advantage of this by modifying our web.config at install time with a file attribute that matches the environment the site is being installed to. We do this with a switch on our installer.

eg;

<appSettings file=".\EnvironmentSpecificConfigurations\dev.config">

<appSettings file=".\EnvironmentSpecificConfigurations\qa.config">

<appSettings file=".\EnvironmentSpecificConfigurations\production.config">

Note:

  • Changes to the .config specified by the attribute won't trigger a restart of the asp.net worker process

Solution 3

Have you looked in to web deployment projects?

http://www.microsoft.com/downloads/details.aspx?FamilyId=0AA30AE8-C73B-4BDD-BB1B-FE697256C459&displaylang=en

There is a version for VS2005 as well, if you are not on 2008.

Solution 4

I'd like to know, too. This helps isolate the problem for me

<connectionStrings configSource="connectionStrings.config"/>

I then keep a connectionStrings.config as well as a "{host} connectionStrings.config". It's still a problem, but if you do this for sections that differ in the two environments, you can deploy and version the same web.config.

(And I don't use VS, btw.)

Solution 5

I use a NAnt Build Script to deploy to my different environments. I have it modify my config files via XPath depending on where they're being deployed to, and then it automagically puts them into that environment using Beyond Compare.

Takes a minute or two to setup, but you only need to do it once. Then batch files take over while I go get another cup of coffee. :)

Here's an article I found on it.

Share:
158,979

Related videos on Youtube

Alexander Prokofyev
Author by

Alexander Prokofyev

Data Scientist, married, have a daughter and two sons.

Updated on December 04, 2020

Comments

  • Alexander Prokofyev
    Alexander Prokofyev over 3 years

    I need to use different database connection string and SMTP server address in my ASP.NET application depending on it is run in development or production environment.

    The application reads settings from Web.config file via WebConfigurationManager.AppSettings property.

    I use Build/Publish command to deploy the application to production server via FTP and then manually replace remote Web.config with correct one.

    Is it possible somehow simplify the process of deployment? Thanks!

  • Thomas
    Thomas over 14 years
    If youd be using VS you could use prebuild-events to copy from a debug.connectionstrings.config or a release.connectionstrings.config like: copy $(ProjectDir)$(ConfigurationName)ConnectionStrings.config $(ProjectDir)ConnectionStrings.config as suggested by Scott. Hanselmann: hanselman.com/blog/…
  • Gary W
    Gary W about 14 years
    This is a good guide for using web deployment projects : johnnycoder.com/blog/2010/01/07/…
  • mo.
    mo. over 10 years
    Note that this doesn't work for old web site projects. Only for web applications. I haven't tried to see if Phil Bolduc's workaround works for web sites, but I suspect it won't, since they don't have project files.
  • Alex from Jitbit
    Alex from Jitbit almost 10 years
    Also note that web.confg transforms work for PUBLISHING only, they don't work if you simply build/run F5 :((((
  • Doug S
    Doug S over 9 years
    If your web.config doesn't contain a Web.Debug.Config and Web.Release.Config, you may need to right-click Web.Config and click Add Config Transforms.
  • Phil
    Phil over 9 years
    This is an excellent answer, especially when you have a large amount of environments and some of the settings for some environments have passwords and similar that you don't want tracked in source control.
  • Perspective
    Perspective over 9 years
    Is there a dynamic way of changing the file path? Based on which server you are on? Just a side note this is working on a old website project, not a web application. So thank you!
  • David Schwartz
    David Schwartz almost 9 years
    There is an attribute restartOnExternalChanges that will treat those files as if they were web.configs. Source: learnable.com/books/…
  • jocull
    jocull over 8 years
    Web deployment projects appear to be VS "Publishing Profiles" now diaryofaninja.com/blog/2012/08/26/…
  • punter
    punter about 6 years
    @Alex : how can we use it for simple build / F5 ?
  • Pierre-Alain Vigeant
    Pierre-Alain Vigeant about 6 years
    @punter That is exactly what SlowCheetah does.
  • Yusha
    Yusha almost 6 years
    @Alex You can't.
  • XoXo
    XoXo over 4 years
    direct link to SlowCheetah: marketplace.visualstudio.com/…
  • Ch'nycos
    Ch'nycos about 4 years
    yes it works but only on deploy, not on compilation, I wish I could simply compile and debug using different config transform environments
  • odelgadillo
    odelgadillo about 3 years
    Buenisimo, gracias!