3

I've been strugling with this for one hour and can't find to simple explanation or at least some Microsoft documentation that states this.

I want to understand behaviour of next files:

  • Web.config
  • Web.Debug.config
  • Web.Release.config
  • Web.Staging.config (I created this using Right click on Web.config -> Add Config Transform)

I added next appSetting in Web.config file:

<configuration>
  ..
  <appSettings>
    <add key="DevDisplayPanel" value="default value" />
  </appSettings>
  ..
</configuration>

In my Web.Debug.config I changed DevDisplayPanel to this:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="DevDisplayPanel" 
         xdt:Transform="Replace" 
         xdt:Locator="Match(key)" 
         value="Debug mode" />
  </appSettings>
</configuration>

I displayed the appSettings item like this:

ViewBag.Test = System.Configuration.ConfigurationManager.AppSettings["DevDisplayPanel"];

I understood that if I run Debug -> Start Debugging in Visual Studio that DevDisplayPanel setting will be read from Web.Debug.config. I can't find a Microsoft documentation stating this.

I tried to run my web application using Debug, Release and Staging solution configuration but they all show DevDisplayPanel value from file Web.config.

Where do files Web.Debug|Release|MyConfiguraton.config come into play. What have I missed and most importantly again this behaviour should be noted in bold somewhere in the docs!!

broadband
  • 3,266
  • 6
  • 43
  • 73
  • What I have done in the past to debug different configurations is to "tokenize" the various config files by putting "__ somevalue __" instead of the actual value. Then have an xml file (or your preference) for each build config with the real values. A simple .exe that replaces tokens is set to run in the after build step and replaces the config file in the bin folder. It's not as bad as it may sound. – Crowcoder Oct 17 '18 at 10:48

2 Answers2

4

Web.config transformations are associated with Publishing your project in a given configuration.

When you debug in Visual Studio, it always uses the plain web.config file, no matter what the Configuration.

You need to choose Build => Publish from the menu options and then run the published application.

ste-fu
  • 6,879
  • 3
  • 27
  • 46
  • You are correct, thank you. But this feature isn't that trivial to use. I makes just more confusion instead of Visual Studio simple usage. I haven't found the doc yet, which states that Debug from Visual Studio ignores Web.xxx.config files. – broadband Oct 17 '18 at 10:22
  • It's more that ms build process is different and doesn't target the pubxml files. I agree it is not intuitive – ste-fu Oct 17 '18 at 10:25
2

Credit to https://gist.github.com/EdCharbeneau/9135216

You can add something like this to the bottom of your .csproj

 <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" />
  <Target Name="BeforeBuild">
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
  </Target>

v16 is visual studio 2019.

Note, that this will overwrite your original web.config with any changes from the transforms. I'm actually talking it over with my team how best to do this in our project, and one solution is to put all your "standard" settings that you debug your project with, but in the web.debug.config. And treat that like your "base" web.config.

Using this snippet has the added benefit of working on your build server prior to a deploy.

TheDudeAbides
  • 124
  • 2
  • 2
  • 9