Web.config Transformation with .NET Framework

Web.Config Transformation can be applied depending on build configuration. When you expand Web.config, you can see Web.Debug.config and Web.Release.config by default.

The transformation in config file is useful while setting different environment configuration. It can be used to change connection string, register error pages and implement logger for tracking errors and information.

This is how we can transform Web.config in .NET Framework.

Here we create a sample ASP.NET Web API project named ConfigTransformation.

Let’s just implement a key named DevDisplayPanel in Web.config first. Then we implement the default configuration key value in the controller.

Add this code in Index method of HomeController assuming the default Controller method on running the project.

var keyvalue = System.Configuration.ConfigurationManager.AppSettings[“DevDisplayPanel”];

Note: You can put the code block anywhere you are compatible with.

The default value in DevDisplayPanel key in Web.config is Debug mode. This is the default implementation of using key value in Web.config.

Let’s go with Web.config transformation.

  • Add a new project configuration with Configuration Manager and create Custom. You can set Copy settings from: <Empty> by default.
  • Right click on Web.config > Add Config Transform in Solution Explorer. You will see Web.Custom.config as a new project configuration added on expanding Web.config.
  • Add different value for key DevDisplayPanel different environments in configuration tag.

Web.Debug.config: Debug mode

    <add key="DevDisplayPanel" value="Debug mode" xdt:Transform="Replace" xdt:Locator="Match(key)" />

Web.Release.config: Release mode

    <add key="DevDisplayPanel" value="Release mode" xdt:Transform="Replace" xdt:Locator="Match(key)" />

Web.Custom.config: Custom mode

    <add key="DevDisplayPanel" value="Custom mode" xdt:Transform="Replace" xdt:Locator="Match(key)" />
  • We have Web.config transform which requires a little change in Web.config file. Right click on ConfigTransformation in Solution Explorer and select Unload Project option. 
  • You can see ConfigTransformation.csproj file. Scroll at the bottom of the ConfigTransformation.csproj and add the block of code at the bottom (within Project tag), and save.

<Target Name="BeforeBuild">
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
<Target Name="AfterBuild">
  • Reload the previously unloaded file with Reload Project Option.
  • Run the project in Debug mode. Now DevDisplayPanel will use value from Debug mode. The value will be read from Web.Debug.config.
  • Let’s set value as Default value for DevDisplayPanel key in Web.config and Save. Run the project in Debug mode.
  • Running the project will use value from Web.Debug.config as you seen in the picture below.
  • You can publish the project in any debug mode, and see the Web.config changes in published location.