211

I need to use different database connection strings and SMTP server addresses in my ASP.NET application, depending on where 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 to somehow simplify the process of deployment? Thanks!

MeSo2
  • 450
  • 1
  • 7
  • 18
Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118

10 Answers10

175

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.

Germstorm
  • 9,709
  • 14
  • 67
  • 83
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
89

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
Jason Slocomb
  • 3,030
  • 1
  • 23
  • 25
  • 3
    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. – Phil Dec 16 '14 at 16:53
  • 2
    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! – Perspective Jan 13 '15 at 18:00
  • 3
    There is an attribute `restartOnExternalChanges` that will treat those files as if they were web.configs. Source: https://learnable.com/books/the-asp-net-2-0-anthology-101-essential-tips-tricks-hacks/preview/how-can-i-simplify-my-web-config-file-a64852b – David Schwartz May 27 '15 at 20:06
24

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.

wulimaster
  • 701
  • 5
  • 7
  • This is a good guide for using web deployment projects : http://johnnycoder.com/blog/2010/01/07/deploy-aspnet-web-applications-with-web-deployment-projects/ – Gary W Mar 28 '10 at 22:23
  • Web deployment projects appear to be VS "Publishing Profiles" now http://www.diaryofaninja.com/blog/2012/08/26/visual-studio-2012-web-deployment-projects-are-dead-ndash-long-live-publishing-profiles – jocull Sep 30 '15 at 04:09
13

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

harpo
  • 41,820
  • 13
  • 96
  • 131
  • 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: http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx – Thomas Jan 04 '10 at 13:42
6

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.

Jeff Sheldon
  • 2,074
  • 1
  • 14
  • 23
5

On one project where we had 4 environments (development, test, staging and production) we developed a system where the application selected the appropriate configuration based on the machine name it was deployed to.

This worked for us because:

  • administrators could deploy applications without involving developers (a requirement) and without having to fiddle with config files (which they hated);
  • machine names adhered to a convention. We matched names using a regular expression and deployed to multiple machines in an environment; and
  • we used integrated security for connection strings. This means we could keep account names in our config files at design time without revealing any passwords.

It worked well for us in this instance, but probably wouldn't work everywhere.

dariom
  • 4,413
  • 28
  • 42
3

The Enterprise Library configuration editor can help you do this. It allows you to create a base config file and then deltas for each environment. You can then merge the base config and the delta to create an environment-specific web.config. Take a look at the information here which takes you through it better than I can.

PhilPursglove
  • 12,511
  • 5
  • 46
  • 68
3

You could also make it a post-build step. Setup a new configuration which is "Deploy" in addition to Debug and Release, and then have the post-build step copy over the correct web.config.

We use automated builds for all of our projects, and with those the build script updates the web.config file to point to the correct location. But that won't help you if you are doing everything from VS.

Cory Foy
  • 7,202
  • 4
  • 31
  • 34
3

This is one of the huge benefits of using the machine.config. At my last job, we had development, test and production environments. We could use the machine.config for things like connection strings (to the appropriate, dev/test/prod SQL machine).

This may not be a solution for you if you don't have access to the actual production machine (like, if you were using a hosting company on a shared host).

Timothy Khouri
  • 31,315
  • 21
  • 88
  • 128
1

You can also use the extension "Configuration Transform" works the same as "SlowCheetah",

Parth Kale
  • 38
  • 3
  • yes it works but only on deploy, not on compilation, I wish I could simply compile and debug using different config transform environments – Ch'nycos Apr 17 '20 at 11:24