How to add config transformations for a custom config file in Visual Studio?

add app.config transform visual studio 2017
app.config transform visual studio 2019
add config transform disabled
add config transform missing
web.config transform on build
web.config transform not working
web config transform for different environments
web.config transform azure devops

The project I am working on involves reading a lot of service endpoints (url) from a config file. Since the list would be quite large I decided to keep them in a custom config file to keep my web.config clean and small. I included the custom section to my web as below:

<mySection configSource="myConfig.config" />

I works perfectly fine.

But the problem of transformation appears during the deployment of the project to different environments. I have three web.config files:

Web.config

Web.Uat.config

Web.Release.config

While the transformation web.config works, the transformations for custom config files fails at deployment.

Is there an way I can transform the custom config file during deployment?


Visual Studio transforms only web.config files by default.

If you need custom config file with transformation for DEV, UAT, PROD, etc environments, then try to

  1. Use custom extensions for Visual Studio like SlowCheetah - XML Transforms for Config transformation preview functionality.
  2. Add for the project from Nuget SlowCheetah to provide build in transformation.

A little bit details:

Add VS Extension SlowCheetah from Extensions and Updates

Right click on your myconfig.config and choose add transorm:

Inside each defined configurations insert your own transormation rulles like that:

<services xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <service name="WebApplication1.Services.Service2" xdt:Transform="Replace" xdt:Locator="Match(name)" >
    <endpoint address="http://localhost:57939/Services/DebugService" behaviorConfiguration="WebApplication1.Services.Service2AspNetAjaxBehavior"
      binding="webHttpBinding" contract="WebApplication1.Services.Service2" />
  </service>
</services>

Hope it was helpful

Configuration Transform, config transformations for two years doing it the hard way by editing the project file and adding the configuration files manually, I thought it would  It adds a lot of transform functionality in general (even to web.config's) so it's a great tool to use. Out of the box, VS2010 will not do that, though. Edit: According to the description for SlowCheetah: "This package enables you to transform app.config or any other XML file based on the build configuration".


I'm going to extend on Andoni Ripoll Jarauta's answer a little.

We were faced with a similar problem. I wanted to pull the connection strings out of the web.config file to limit merge conflicts. I also wanted create a "release" config containing static information when publishing.

...simple enough. Create a custom config file, webdb.config, and update the web.config file.

Ex. web.config

<connectionStrings configSource="WebDB.config"/>

wedbdb.config (xml version="1.0" is required for transformation)

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
</connectionStrings>

Next add transformation files for webdb.config

WebDB.Debug.config example:

<?xml version="1.0" encoding="utf-8"?>

<connectionStrings xdt:Transform="Replace" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="PRRADDataContainer" connectionString="metadata=~/PRRADData.csdl|~/PRRADData.ssdl|~/PRRADData.msl;provider=System.Data.SqlClient;provider connection string=';Data Source=localhost;Initial Catalog=;User ID=;Password=;multipleactiveresultsets=True;App=EntityFramework';" providerName="System.Data.EntityClient" />
    <add name="MyConnectionString" connectionString="Data Source=localhost;Initial Catalog=;Persist Security Info=True;User ID=;Password=;" providerName="System.Data.SqlClient" />
</connectionStrings>

WebDB.Release.config example:

<?xml version="1.0" encoding="utf-8"?>

<connectionStrings xdt:Transform="Replace" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="PRRADDataContainer" connectionString="metadata=~/PRRADData.csdl|~/PRRADData.ssdl|~/PRRADData.msl;provider=System.Data.SqlClient;provider connection string=';Data Source=prod_server;Initial Catalog=;User ID=;Password=;multipleactiveresultsets=True;App=EntityFramework';" providerName="System.Data.EntityClient" />
    <add name="MyConnectionString" connectionString="Data Source=prod_server;Initial Catalog=;Persist Security Info=True;User ID=;Password=;" providerName="System.Data.SqlClient" />
</connectionStrings>

Next we need to add an after-build event. This is created by simply editing the CSPROJ file.

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterBuild">
    <TransformXml Source="WebDB.config" Transform="WebDB.$(Configuration).config" Destination="WebDB.config" />
</Target>

Now when I run locally I'll get WebDB.Debug.config and when I publish my code I just need to make sure to select "Release" as the configuration source. In both cases the WebDB.config file will be updated with the corresponding file when you build.

NOTE: make sure you set the webdb.config, webdb.debug.config, and webdb.release.config to "Do not copy" for the "Copy to Output Directory" option.

Hope this helps!

ASP.NET Web Deployment using Visual Studio: Web.config File , You can create transformation files for custom build configurations by right-​clicking the Web.config file and choosing Add Config Transforms from  You can create transformation files for custom build configurations by right-clicking the Web.config file and choosing Add Config Transforms from the context menu. For this tutorial you don't need to do that, and the menu option is disabled, because you haven't created any custom build configurations.


I have been using SlowCheetah but I found something that I think is more elegant. Just telling to the build to generate the .config depending on the build configuration.

Having a app.Release.config in your project (or many more depending on you deployment needs) you just need to edit the project file (the .csproj one if you program in C#). Find the end of it, between the last </ItemGroup> and </Project> and add:

  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="AfterBuild">
    <PropertyGroup>
      <OutputTypeName>$(OutputType)</OutputTypeName>
      <OutputTypeName Condition="'$(OutputTypeName)'=='Library'">dll</OutputTypeName>
      <OutputTypeName Condition="'$(OutputTypeName)'=='Module'">dll</OutputTypeName>
      <OutputTypeName Condition="'$(OutputTypeName)'=='Winexe'">exe</OutputTypeName>
    </PropertyGroup>
    <TransformXml Source="Config\app.config" Transform="Config\app.$(Configuration).config" Destination="$(OutputPath)\$(AssemblyName).$(OutputTypeName).config" />
  </Target>
</Project>

Save and reload from VisualStudio. Compile in Release mode and check the bin/Release folder on your <MyProject>.config file the transformation is done.

This example applies to Exe and Dll files and any VisualStudio version because includes this post help

Transform web.config, How to enable transformations on build with Visual Studio Unload the project; Edit .csproj; Append figure 1 to the end of the file just before Config > Add Config Transformation; Overwrite the web.base.config with the First, we extend the "BuildDependsOn" property with our custom target for applying the transform. Whatever you choose to do, remember that anything in the main "App.Config" file will be the default if no rules to perform any transforms can be found. Open "App.Config" in Visual Studio and we'll add a dummy connection string, which we'll call our Default Connection string. Make sure your "App.Config" includes this section:


How to enable transformations on build with Visual Studio · GitHub, The best tutorial for web.config/app.config transformations. Want to insert an element beneath another element? After restarting Visual Studio, right-clicking an App.config file will now show the Add Transform option:. Add a new Configuration Name: Base. Copy settings from: Release; Copy the contents of your web.config; Right click Web.Config > Add Config Transformation; Overwrite the web.base.config with the contents of your clipboard; From now on Web.Config will be overwritten using transformations. For settings that apply to all cofigurations use Base


Web.config transformations, NET are accompanied by some kind of configurations file. You also can add custom profiles to this, too, so you could, for example, have three Open "App.​Config" in Visual Studio and we'll add a dummy connection string,  Right click link App.config file in solution explorer and select Add Config Transforms. Click Yes button, if you select No button concrete(not linked) config files will be created which suits certain use case. Notice that you have new App.$$$.config files under App.config. corresponding to source App.config files.


Using Config Transformations in a non-Web Project, work well. Here is how to enable web.config transforms for App.config files too. Steps listed below and XML-blocks are tested on Visual Studio 2012 only. Insert-transform with appSettings block is maybe simplest one. Go to Build->Configuration Manager Click on the Active solutions configuration and select "New". Enter the name of your new configuration and click Save. Right click on your Web.config file and select Add Config Transform.


Using web.config transforms with App.config files, Let's take a step by step look to create a single transformation file for the Config files is the main project's configuration file. Now, move to the end of the file, to the closing </Project> tag, and include the following custom  A transform file is an XML file that specifies how the Web.config file should be changed when it is deployed. Transformation actions are specified by using XML attributes that are defined in the XML-Document-Transform namespace, which is mapped to the xdt prefix. The XML-Document-Transform namespace defines two attributes: Locator and Transform.