How to change the value of attribute in appSettings section with Web.config transformation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In classic ASP.NET projects, Web.config transforms let you change configuration values per environment during publish or build. For appSettings, the usual task is to locate an existing add entry by key and replace its value attribute. The core XDT pattern is Locator="Match(key)" plus Transform="SetAttributes(value)".
Base Web.config
Suppose the main configuration file contains this setting:
During deployment, you might want the release build to point to a production URL instead.
Transform File Structure
Environment-specific transforms are usually stored in files such as Web.Release.config or Web.Debug.config. The transform file must declare the XDT namespace.
This tells the transform engine:
- find the
addelement whosekeyattribute equalsApiBaseUrl - update only the
valueattribute
That is the standard answer for changing an existing app setting value.
Why Match(key) Matters
Without a locator, the transform engine has no precise way to identify which add element you mean. Match(key) is the normal choice because key is the identity field for appSettings entries.
This changes only the value attribute of the matching element and leaves other settings untouched.
SetAttributes Versus Replace
If you only need to modify one attribute, use SetAttributes. If you use Replace, you replace the entire matched element.
Targeted update:
Full element replacement:
Replace is more disruptive and easier to misuse. Prefer SetAttributes(value) when you only want to change the value field.
Adding a Setting If It Does Not Exist
Sometimes the desired key is missing from the base file. In that case, you need an insert transform instead of an attribute update.
Do not expect SetAttributes to create a missing element. It only updates an existing match.
Multiple Attribute Updates
You can change more than one attribute by listing them in SetAttributes.
For plain appSettings, that is less common because the typical shape is just key and value, but the syntax is useful in other XML sections too.
How to Verify the Result
After publishing or transforming, inspect the generated configuration file rather than assuming the transform ran correctly. Common checks include:
- was the right build configuration used
- did the key exist in the base file
- did the output file contain the expected value
If the published config still shows the original setting, the issue is usually one of these:
- missing
xmlns:xdt - incorrect locator
- transform file not applied during publish
Example: Release Transform
Here is a complete release transform for two settings:
This keeps the transform file focused and readable, which is important when multiple environments are maintained over time.
Common Pitfalls
The most common mistake is using SetAttributes without Match(key), which leaves the transform engine unable to target the correct element. Another is using Replace when only the value attribute should change. Developers also sometimes forget the XDT namespace declaration, in which case the transform attributes are ignored entirely. Finally, SetAttributes does not create missing keys, so it fails silently if the base element is not there.
Summary
- Use
xdt:Locator="Match(key)"to identify the correctappSettingsentry. - Use
xdt:Transform="SetAttributes(value)"to change only the value attribute. - Use
Insertinstead ofSetAttributeswhen the key does not exist yet. - Prefer targeted attribute updates over full
Replacewhen possible. - Verify the transformed output after publish rather than assuming the change was applied.

