Home > database >  Variable substitution does not work in Web.Config section
Variable substitution does not work in Web.Config section

Time:08-19

I have this custom section in Web.config

<serviceWorker PriorityCronExp="__serviceWorker_PriorityCronExp__" ScheduledCronExp="__serviceWorker_ScheduledCronExp__" />

I want to replace the variables in my release, I have configured the variables and checked "XML Variable subsitution", but my release is not detecting the variables the vars are not replaced. Other variables have been replaced correctly (defined in appSettings). Is there a way to get the variables replaced without having to modify my Web.Config?

P.S. I am targeting .Net Framework 4.8

CodePudding user response:

I didn't see the whole structure of your Web.config, But I can probably tell what happened on your side.

The feature 'XML variable substitution' only works for applicationSettings, appSettings, connectionStrings, and configSections. Only these will work.

It is clearly clarify here:

https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/transforms-variable-substitution?view=azure-devops&tabs=Classic#xml-variable-substitution

This feature enables you to modify configuration settings in configuration files (*.config files) inside web packages and XML parameters files (parameters.xml). In this way, the same package can be configured based on the environment to which it will be deployed.

Variable substitution takes effect only on the applicationSettings, appSettings, connectionStrings, and configSections elements of configuration files. If you are looking to substitute values outside of these elements you can use a (parameters.xml) file, however you will need to use a 3rd party pipeline task to handle the variable substitution.

This is how it design.

If I misunderstand something, please feel free to let me know.

CodePudding user response:

Is there a way to get the variables replaced without having to modify my Web.Config?

Yes. I suggest that you can add RegEx Match & Replace task from RegEx Match & Replace to your Pipeline to modify the web.config file.

This task will use regular expressions to match fields in the file.

steps:
- task: RegExMatchReplace@2
  displayName: 'RegEx Match & Replace'
  inputs:
    PathToFile: web.config
    RegEx: 'PriorityCronExp="(?:[^\\"]|\\\\|\\")*" ScheduledCronExp="(?:[^\\"]|\\\\|\\")*"'
    ValueToReplace: 'PriorityCronExp="$(variabename1)" ScheduledCronExp="$(variablename2)"'

Then the value will update.

You can use this site to convert the regular expressions : Regex Generator

  • Related