82

How can I merge and make use of Web.debug.config in visual studio 2010 built-in debugger?

stacker
  • 14,641
  • 17
  • 46
  • 74

7 Answers7

95

This is a known bug. That feature can be used right now only as part of the deploy process.

https://connect.microsoft.com/VisualStudio/feedback/details/523221/have-web-debug-config-apply-during-development

Please upvote it, if you encounter this too, so this will be fixed ASAP.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
stacker
  • 14,641
  • 17
  • 46
  • 74
  • 18
    For the record this is not a bug, its by design. These files are for package/publish. But that should not discourage anyone from voting the item up. – Sayed Ibrahim Hashimi Oct 21 '10 at 18:58
  • 29
    Well to me it is a bug, otherwise what is the purpose of a Debug / Release transform on a localserver? Sometimes designs are erroneous, that doesn't mean it is correct :) – Lord of Scripts Jun 15 '12 at 05:13
  • 4
    The problem comes when you use Github and need to ignore the web.config when syncing. But that defeats the purpose of having a back up - I mean that's why I use Github. Now add AppHarbor to the scenario and you have to support two types web.config files, because you do want your apikeys published on Github, so you have to substitute values in your configs before sync and publishing. It's a problem. Forget to substitute your values in the config and sync by mistake, and guess what? Your apikeys just made into a public repository. Yikes, what a problem. – David Robbins Sep 25 '12 at 13:56
  • 9
    The link to MS Connect is broken – Michael Freidgeim May 25 '13 at 18:28
  • 20
    @LordofScripts: when a bug is closed "as designed" I sometimes want to say "then there's a bug in your design!" – Roy Tinker Jan 27 '14 at 23:35
36

This is actually quite simple to do and, believe it or not, it seems this is the way VS is designed to work.

Add the following lines verbatim right before the closing "Project" tag of the .csproj file of the project that contains web.config.

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="Transform">
    <MakeDir Directories="obj\$(Configuration)" Condition="!Exists('obj\$(Configuration)')" />
    <TransformXml Source="Web.Config" Transform="Web.$(Configuration).config" Destination="obj\$(Configuration)\Web.config" StackTrace="true" />
</Target>

Put the following lines verbatim to the post-build event in the project properties of the project that contains the web.config file. Do this for each build configuration you want the transformations to run for.

"$(MSBUILDBINPATH)\msbuild" "$(ProjectPath)" /t:Transform /p:Configuration=$(ConfigurationName);Platform=AnyCPU
xcopy "$(ProjectDir)obj\$(ConfigurationName)\Web.Config" "$(ProjectDir)". /F /R /Y
stuckintheshuck
  • 2,449
  • 3
  • 27
  • 33
  • 1
    I would only recommend adding double quotes around $(ProjectDir) and $(ProjectPath) to avoid errors in case your project path contains spaces. – Alexander Prokofyev Apr 03 '13 at 09:49
  • 1
    It's better to use , as it was suggested in http://stackoverflow.com/a/10506476/52277 – Michael Freidgeim Jan 14 '14 at 05:35
  • I am able to get the transforms to work using this approach but I am unable to hit my breakpoints. I have debug set to true but I never hit the breakpoint and i get the following message "The breakpoint will not currently be hit. No symbols have been loaded for this document.". This only happens for my custom configurations and works just fine if I set my configuration to "Debug". Any ideas? – mithun_daa Mar 03 '14 at 20:50
  • 1
    I had to add double quotes around $(MSBUILDBINPATH) to make it work, otherwise it exits with code 9009 – yakya Dec 05 '14 at 08:49
  • 7
    Note that this replaces the original `Web.config`, so *all* your transforms must be "idempotent" (can be reapplied over the *results* of any of the other transforms). It will also prevent you from keeping your sanity should you put the file in source control (as you probably should). Overall a "simple" hack that shifts the complexity/pain to the next set of problems. Good if you can tackle it "better" there, bad if you don't. Also consider using $(VisualStudioVersion) instead of hard-coding the VS version (you *will* forget to change it). – tne Jan 28 '15 at 10:48
  • Thanks, this worked for me also. So now I can see the changes when I am debugging. – yogibear Apr 27 '23 at 16:19
19

I had solved this in a simpler way, by adding this at the end of the .csproj file, right before the tag. This is similar to keitn's answer, with the difference that it doesn't use a post build event.

<Target Name="BeforeBuild">
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
Marius Ologesa
  • 311
  • 2
  • 4
2

I know this is old, but I'm facing the same problem. We have Test, Staging, Live configs that replace endpoints, connection strings etc. from the default Web.config

However I would do the following:

  • Right click on the desired transform config (e.g. Web.Live.config)
  • Click on "Preview Transform"
  • Copy everything from right (it's how the Web.config looks with the transformation)
    • CTRL+A + CTRL+C
  • Open Web.config file (default one)
  • Select everything (CTRL+A) and paste it in (CTRL+V)
  • Run

It's not that many steps and is done pretty quickly when you get a hang of it. Hope this helps. :)

Gaui
  • 8,723
  • 16
  • 64
  • 91
  • If I have understood correctly then this is a destructive change to the original web.config file. In the context of the question these steps would need to be performed each time a debug session is started. – Red Taz Mar 19 '15 at 13:42
2

I didn't want to update the web.config in my project just the one that ends up in the bin folder so here is how I did it.

Add the following to the end of .csproj (just before the final closing project tag)

<Target Name="Transform">
    <MakeDir Directories="bin" Condition="!Exists('bin')" />
    <TransformXml Source="Web.Config" Transform="Web.$(Configuration).config" Destination="bin\$(TargetFileName).config" StackTrace="true" />
  </Target>

Then add the following post build step

"$(MSBUILDBINPATH)\msbuild" "$(ProjectPath)" /t:Transform /p:Configuration=$(ConfigurationName);Platform=AnyCPU

This means that when you build a transform takes place from the debug/release config to WebsiteName.Config file in the output bin directory thus not interfering with the main web.config in the project.

keitn
  • 1,288
  • 2
  • 19
  • 43
  • 2
    This does transform the config in the bin directory, unfortunately this config is never used. The config loaded is actually the Web.Config in the project folder, not the one in the bin directory. – Mick Jan 13 '16 at 09:24
2

After reading many similar posts and having problems with files not being able to be overwritten or web.config not being accessible because it is read only this is what I got working for me:

  <Target Name="BeforeBuild" Condition="$(Configuration) == 'MyAltDebugConfiguration'">
    <ItemGroup>
      <OriginalWebConfig Include="$(ProjectDir)Web.config"/>
      <TempWebConfig Include="$(ProjectDir)TempWeb.config"/>
    </ItemGroup>
    <Exec Command="&quot;$(DevEnvDir)tf.exe&quot; checkout &quot;$(ProjectDir)Web.config&quot;" />
    <Copy SourceFiles="@(OriginalWebConfig)" DestinationFiles="@(TempWebConfig)" />
    <TransformXml Source="$(ProjectDir)TempWeb.config"
                            Transform="Web.$(Configuration).config"
                            Destination="Web.config" />
  </Target>

Notes:

This runs as the BeforeBuild target.

I only want it to run under a certain configuration (an alternative debug environment) and so that is why I have the Condition. When deploying via web deploy the publishing target kicks in and I don't need this target to run.

I don't want to have to remember to check out web.config (only to undo it when I am done) so I check web.config out before beginning the transform. If you aren't using TFS you can remove this line.

Because VS (2010) \ msbuild doesn't want to let go of the Source web.config I use a temp file (thanks to this article for the info: http://www.diaryofaninja.com/blog/2011/09/14/using-custom-webconfig-transformations-in-msbuild)

I tried adding a command to delete the TempWeb.config but VS \ msbuild doesn't want to let go of it. I can live with it as it doesn't get added to TFS.

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
  • I have this error `The command ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\tf.exe" checkout "C:\_Code\RTS\SCTimeSheet\Web.config"" exited with code 9009`. Any ideas? – sky91 Jun 10 '20 at 05:53
-1

@ologesa: Your solution needs write access to the original Web.config (you must check-out in TFS). The better solution is to directly generate the Web.config in the bin folder like keitn does this. When we combine keitn's and your solution we get this one:

<Target Name="BeforeBuild">
    <Message Text="Transforming Web.config from Web.$(Configuration).config" Importance="high" />
    <MakeDir Directories="bin" Condition="!Exists('bin')" />
    <TransformXml Source="Web.Config" Transform="Web.$(Configuration).config" Destination="bin\$(TargetFileName).config" StackTrace="true" />
</Target>
Mickey Mouse
  • 723
  • 6
  • 4
  • 3
    Would be nice but as Mick commented; this file is not used. How do I get the application or IIS use this config file? – HMR Mar 02 '16 at 04:28