Search code examples
c#asp.netweb-configweb-config-transform

ASP.Net Web.Config transform naming issues


I have an ASP.Net project which utilizes the web.config transforms. To facilitate this I have built a structure like so:

web.base.config
    web.debug.config
    web.QA.config
    web.release.config
    web.config

The web.base.config contains the untransformed base of the web.config data, while the web.[ENVIRONMENT].config contains the transform data based upon the current build configuration. The web.config contains the transform result that gets loaded at runtime.

This works really well for us and has been very nice for staging changes.

However there's a small issue that I've thus far been working around manually: When I update my NuGet dependencies they apply their transforms to the web.config file, which upon build then gets blown away with the transformations from my other configs.

I have looked into a few different ways of fixing this and I haven't had any luck...Any ideas?

For the record, this is how I achieved the above web.config schema (in the project file):

  <ItemGroup>
    <None Include="Web.base.config">
    </None>
    <None Include="Web.base.Local.config">
      <DependentUpon>Web.base.config</DependentUpon>
    </None>
    <None Include="Web.base.Debug.config">
      <DependentUpon>Web.base.config</DependentUpon>
    </None>
    <None Include="Web.base.Release.config">
      <DependentUpon>Web.base.config</DependentUpon>
    </None>
    <None Include="Web.base.QA.config">
      <DependentUpon>Web.base.config</DependentUpon>
    </None>
    <Content Include="Web.config">
      <DependentUpon>Web.base.config</DependentUpon>
    </Content>
  </ItemGroup>
  <Target Name="BeforeBuild">
    <Exec Command="attrib -r Web.config" />
    <TransformXml Source="web.base.config" Transform="web.base.$(Configuration).config" Destination="web.config" StackTrace="true" />
  </Target>

Solution

  • By design, web.config should contain all your base/default/debug values. You then "overrride" those with transforms (based on environment, configuration or whatever criteria you choose). I don't think there is any setting that'd let you say that use web.base.config instead of web.config. Your best bet is to follow the convention.

    E.g. web.config

    <add key="IsDebug" value="true" /> 
    

    web.release.config

    <add key="IsDebug" value="false"  xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />