4

I am planning to use azure devops release pipeline to deploy a .NET web app into test, uat and prod environments.

I have previously worked on projects which had the appsettings.json file and used variable substitution approach as shown in screenshot below:

enter image description here

When doing the variable replacement for a .NET web app containing the web.config, I am exploring the following options:

  1. Generate web.config parameters section which is shown above the JSON section marked above. But the description says that it is meant for Python, Node.js, Go and Java apps. It doesn't mention .NET.
  2. In the web.config file, in the value I can add something like XXMARKERXX and then via Powershell script maybe replace the above with the contents from pipeline variable?
variable
  • 8,262
  • 9
  • 95
  • 215

1 Answers1

5

The web.config file is XML format type file. So you couldn't use JSON variable substitution option in task to update the value.

Here are the methods to update the value in Web.config file:

1.You can use the XML variable substitution option in the Azure App service deploy task or IIS deploy task.

enter image description here

For more detailed steps, refer to this doc: XML variable substitution

2.You can use PowerShell script to modify the value. But you don't need to add any additional characters in web.config file.

Here is the PowerShell script sample:

$myConnectionString = "test";

$webConfig = '$(build.sourcesdirectory)\Web.config'

Function updateConfig($config) 
{ 
$doc = (Get-Content $config) -as [Xml]
$root = $doc.get_DocumentElement();
$activeConnection = $root.connectionStrings.SelectNodes("add");
$activeConnection.SetAttribute("connectionString", $myConnectionString);
$doc.Save($config)
} 

updateConfig($webConfig)

3.When you add the mark: #{..}# in web.config file, you can try to use the Replace Token task from Replace Tokens Extension.

Refer to the example in this ticket:How to perform XML Element substitution in web.config using Replace Tokens?

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 1
    Nice. In the 3rd approach is it important to follow the syntax with `#` at start/end? `#{..}#`? – variable Jun 20 '22 at 04:48
  • 1
    Yes. This is important if you choose 3rd method. And you can also custom the syntax in the task. This is supported by replace token task – Kevin Lu-MSFT Jun 20 '22 at 05:02