Getting Started with Web Config Modifier

Web Config Modifier is designed to have you up and running within minutes. It’s an extremely simple console application that simply drops into your build deployment scripts, either using an Exec task in MSBuild or NAnt, or by calling it directly from a batch file or PowerShell script as executed by TFS Deployer.

Web Config Modifier takes its instructions from command-line parameters and its values from STDIN. This allows replacement of both individual values and entire configuration sections.



Just download, extract it to somewhere accessible (either in the path, or under version control in your BuildProcessTemplates\Deployment directory if you’re using TFS Deployer) and you’re done. There’s only a single executable so drop it anywhere you need it.



  echo <value> | WebConfigModifier.exe <path to web.config> /element <xPathToElement>
  type <filename> | WebConfigModifier.exe <path to web.config> /element <xPathToElement>
  echo <value> | WebConfigModifier.exe <path to web.config> /attribute <xPathToElement> <attributeName>


  To replace individual connection strings, replace the providerName and connectionString attributes:
    echo System.Data.SqlClient | WebConfigModifier.exe Samples\Mvc2.Vanilla.Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] providerName
    echo "data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|different_aspnetdb.mdf;User Instance=true" | WebConfigModifier.exe Samples\Mvc2.Vanilla.Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] connectionString

  To replace the entire connectionStrings document element from a file:
    type Samples\connectionStrings.xml | WebConfigModifier.exe Samples\Mvc2.Vanilla.Web.config /element /configuration/connectionStrings


I have a connection string called “MyDatabaseConnection” and I want to change it based on whether I’m deploying to my CI or Nightly server. Help!

Easy. In your deployment script, you’ll already know which server you’re connecting to. In your CI server section, you’ll want something like this:

echo System.Data.SqlClient | WebConfigModifier.exe \\CI_SERVER\PathToApp\Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] providerName
echo "data source=CI_SERVER\MyAppInstance;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|different_aspnetdb.mdf;User Instance=true" | WebConfigModifier.exe \\CI_SERVER\PathToApp\Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] connectionString

and in your Nightly section, you’ll want something like this:

echo System.Data.SqlClient | WebConfigModifier.exe \\NIGHTLY_SERVER\PathToApp\Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] providerName
echo "data source=NIGHTLY_SERVER\MyAppInstance;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|different_aspnetdb.mdf;User Instance=true" | WebConfigModifier.exe \\NIGHTLY_SERVER\PathToApp\Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] connectionString

Pay attention to the highlighted parts Smile


That’s ugly. Can we make it neater?

Of course. It’s ugly because we’re sending the entire connection string as part of the command line. That’s bad for two reasons: 1) it makes the script harder to read, and 2) it doesn’t let us separate our connection strings from our deployment scripts.

Here’s an easier way:

type CIProviderName.txt | WebConfigModifier.exe \\CI_SERVER\PathToApp\Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] providerName
type CIConnectionString.txt | WebConfigModifier.exe \\CI_SERVER\PathToApp\Web.config /attribute /configuration/connectionStrings/add[@name='ApplicationServices'] connectionString

It would, of course, be a good idea to put these files under version control as well. TFS Deployer will check out anything in the Deployment folder underneath the BuildProcessTemplates folder, so there’s a good place to start looking.


Can I replace the entire connectionStrings section?

type CIConnectionStringSection.xml | WebConfigModifier.exe \\CI_SERVER\PathToApp\Web.config /element /configuration/connectionStrings


Can I replace other configuration sections?

Of course. All you need to do is change the XPath expression from matching the /configuration/connectionStrings/add elements to the element you’d like to modify.

Last edited Oct 25, 2010 at 12:54 AM by uglybugger, version 9


No comments yet.