Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended way to update the web.config values using release pipeline variables?

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?
like image 795
variable Avatar asked Sep 11 '25 08:09

variable


1 Answers

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?

like image 155
Kevin Lu-MSFT Avatar answered Sep 14 '25 03:09

Kevin Lu-MSFT