Web.config transforms and git. Ending the frustration.

In the world of MVC projects, managing web.config transformations can often be a tricky task, especially when working with source control systems like Git. A common issue that developers face is that every time a web transform is run, the web.config file changes are being tracked by Git. This can be frustrating as these changes are made before runtime and often need to be rolled back.

To address this issue, I came up with a solution that not only resolved the problem but also improved the overall build process of my project. Here’s the process I followed:

The Problem

The root of the problem was that the web.config file was being transformed during the build process, and these changes were being tracked by Git. This is a common issue in .NET projects where different configurations are needed for different environments (like development, staging, and production). The web.config file often contains sensitive information like database connection strings, which should not be tracked in source control.

The Solution

The solution I found involves using the Web Publishing Pipeline (WPP) in .NET, which is a set of MSBuild targets and tasks that handle packaging and deployment of web applications. Specifically, I used the TransformXml task in a custom MSBuild target to apply the transformations to a temporary web.config file, which is not tracked by Git.

Here’s how I set it up:

Create a Template Web.config File: This is your base configuration file. It should contain all the settings that are common across all environments. Name this file template.web.config and include it in your source control.

<configuration>
  <appSettings>
    <add key="Setting1" value="BaseValue" />
  </appSettings>
  <connectionStrings>
    <add name="MyDB" connectionString="Data Source=BaseSQLServer;Initial Catalog=MyBaseDB;Integrated Security=True" />
  </connectionStrings>
</configuration>

Create the .wpp.targets File: I created a file named ProjectName.wpp.targets in the same directory as my .csproj file, where ProjectName is the name of my project. This file contains an MSBuild target that runs before the app is run in Visual Studio and applies the transformations from web.[Configuration].config to a temporary web.config file.

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="CopyWebTemplateConfig" BeforeTargets="PrepareForBuild">
    <Copy SourceFiles="template.web.config"
          DestinationFiles="web.config"/>
  </Target>
  
  <PropertyGroup>
    <PrepareForRunDependsOn>
      $(PrepareForRunDependsOn);
      UpdateWebConfigBeforeRun;
    </PrepareForRunDependsOn>
  </PropertyGroup>

  <Target Name="UpdateWebConfigBeforeRun">
    <Message Text="Configuration: $(Configuration): web.$(Configuration).config"/>
    <TransformXml Source="web.config"
              Transform="web.$(Configuration).config"
              Destination="web.config" />
  </Target>
</Project>

Update the .gitignore File: I updated my .gitignore file to ignore the temporary web.config file. This ensures that the changes to this file are not tracked by Git.

If your file is already tracked, you may need to run this command

git rm --cached web.config

Run the App in Visual Studio: When I run my app in Visual Studio, the transformations are applied to the temporary web.config file, and these changes are not tracked by Git.

This solution has the added benefit of keeping the original web.config file clean and free of environment-specific settings, which makes it easier to manage and understand.

I hope this solution proves helpful to others facing similar issues. As always, happy coding! 🚀


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *