Web config transformation fails

102 Views Asked by At

We have asp.net web project which we are migrating to .NET SDK-style project with centralized nuget package management.

Template that we are using is <Project Sdk="MSBuild.SDK.SystemWeb/4.0.88"> and we have a Web.Config file with three transformations (Debug, Release, Test). Build Action is set to content, and Copy to Output Directory is set to None.

VS build works just fine, however when we try our release build, we get following message:

PreTransformWebConfig:
  Found The following for Config tranformation:
  Web.config, Web.config
  Skip copying Web.config to ...\Release\TransformWebConfig\original\W
  eb.config, File ...\Release\TransformWebConfig\original\Web.config i
  s up to date

And build fails with:

Transforming Source File: ...\Web.config;Web.config
C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\v17.0\Web\Microsoft.Web.Publi
shing.targets(1552,5): error : Could not open Source file: Could not find file '...\Web.config;Web.config'. [...Web.API.csproj]

Build command:

msbuild /p:configuration=release /p:DeployOnBuild=true /p:DebugType=None /p:SkipInvalidConfigurations=true /p:WarningLevel=0 

How can I rectify this issue? This is a .net framework project and having ability to transform web.config is quite important for us.

2

There are 2 best solutions below

0
On

This happened when a project was migrated to SDK-style, while keeping the explicit Content items.

Fix: Add <EnableWebFormsDefaultItems>false</EnableWebFormsDefaultItems> or remove the explicit items.

Original answer copied from: https://github.com/CZEMacLeod/MSBuild.SDK.SystemWeb/issues/60

0
On

In the end we had some mess with CSPROJ file itself... What works for us is:

<ItemGroup>
    <None Include="Web.Debug.config">
      <IsTransformFile>true</IsTransformFile>
      <DependentUpon>Web.config</DependentUpon>
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </None>
    <None Include="Web.Release.config">
      <IsTransformFile>true</IsTransformFile>
      <DependentUpon>Web.config</DependentUpon>
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </None>
    <None Include="Web.Test.config">
      <IsTransformFile>true</IsTransformFile>
      <DependentUpon>Web.config</DependentUpon>
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </None>
</ItemGroup>
 .....
<ItemGroup>    
    <Content Update="Web.config">
        <CopyToOutputDirectory>Never</CopyToOutputDirectory>
        <TransformOnBuild>true</TransformOnBuild>
    </Content>
    <Content Update="Web.Debug.config">
        <IsTransformFile>true</IsTransformFile>
        <DependentUpon>Web.config</DependentUpon>
        <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
    <Content Update="Web.Release.config">
        <IsTransformFile>true</IsTransformFile>
        <DependentUpon>Web.config</DependentUpon>
        <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
</ItemGroup>