How to transform the App Settings in different Web.config files based on the deployment environment

Transforming a Web.config file based on the environment where an application is deployed is a common practice in .NET Framework applications to manage different settings like database connection strings, URLs, or other configuration values. In this article, we will see how to achieve this using the configuration transformation feature in Visual Studio.

Add a new Web.config file into your project

If your .NET Framework project does not already contain a Web.config file, add a new one. This file can be empty; it will be automatically populated with data in the next steps.

Create a new Web.Template.config file

This file will be the base for your Web.config file. file. Here you can store all the configuration you need, for example, the sections <appsettings>, <system.webServer>, <system.web>, etc. You can then override this configuration by using transformations, the topic of this article.

Create environment-specific config files

Now we will create environment-specific Web.config transformation files. These files typically follow the naming convention Web.<Environment>.config. For this tutorial we create a new Web.Development.config and a web.Production.config.

Connect the config files to the Web.config

Open the .csproj file in Visual Studio and add the following lines:

<None Include="Web.Template.config">
    <DependentUpon>Web.config</DependentUpon>
</None>
<Content Include="Web.Development.config">
    <DependentUpon>Web.config</DependentUpon>
    <CopyToOutputDirectory>Always<CopyToOutputDirectory>
</Content>
<Content Include="Web.Production.config">
    <DependentUpon>Web.config</DependentUpon>
    <CopyToOutputDirectory>Always<CopyToOutputDirectory>
</Content>

Adding Transformations

In each of the two environment-specific files, you will specify the transformations you want to apply to the base Web.config file. The most common transformations are replacing or removing elements. Here’s an example for Web.Development.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ApiUrl" value="https://dev.example.com/api" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

and for Web.Production.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ApiUrl" value="https://prod.example.com/api" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

This configuration will replace the default value of ApiUrl defined in Web.Template.config based on the environment. Pay attention to the attributes xmlns:xdt, xdt:Transform and xdt:Locator used.

Add a new AfterBuild rule in your .csproj file

Now we are ready to automate the applying of the transformations into the Web.config file.

Edit the .csproj file one more time and add the following snippet:

<Target Name="AfterBuild">
  <TransformXml Source="Web.config" 
                Transform="Web.$(Configuration).config" 
                Destination="Web.config" />
</Target>

This will ensure that when you deploy your application with a Pipeline the Web.config file will contain the correct transformations of the app settings based on the environment you are currently deploying your application to.

Conclusion

Transforming Web.config files based on the environment is a powerful feature that helps manage environment-specific settings in a clean and efficient manner. If you have any further questions or need more detailed assistance, feel free to ask!

comments powered by Disqus