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:
When doing the variable replacement for a .NET web app containing the web.config
, I am exploring the following options:
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With