17

I've tried several times to use a similar technique as "conditional references" for conditional content.

Content entries in the Visual Studio Project file such as "web.config" I do not want included when I publish the website.

I've tried a few things like...

<Choose>

    <When Condition="$(Configuration) != 'Release'">
        <ItemGroup>
            <Content Include="web.config">
                <SubType>Designer</SubType>
                <CopyToOutputDirectory>Always</CopyToOutputDirectory>
                 </Content>
        </ItemGroup>
    </When>
    <Otherwise>
        <ItemGroup>
        </ItemGroup>
    </Otherwise>

</Choose>

But this doesn't work. Any ideas? Or have you encountered this before and solved it?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
tentux
  • 253
  • 1
  • 2
  • 8

2 Answers2

30

I believe you can just add the Condition to the ItemGroup... Example:

    <ItemGroup Condition="'$(Configuration)' != 'Release'"> 
        <Content Include="web.config"> 
            <SubType>Designer</SubType> 
            <CopyToOutputDirectory>Always</CopyToOutputDirectory> 
             </Content> 
    </ItemGroup> 

Note the ticks around '$(Configuration)' in the condition. Those are very necessary.

Nick Nieslanik
  • 4,388
  • 23
  • 21
  • 1
    I have just tried this and I can't get it to work. I've also tried Condition="'$(Configuration)' == 'Debug'", but still no joy. When I switch configurations the files simply stay put in the project. I've also tried switching configurations and then unloading and reloading the project, but still those files are included despite which configuration I choose. – tentux Nov 15 '11 at 17:43
  • I have also just tried creating a new Console Application so make sure the more complex solution wasn't throwing a spanner in the works. But still it does not work: No matter what configuration I choose these files are present in the project. – tentux Nov 15 '11 at 17:54
  • 1
    I've just released though that when BUILDING in release I get a compilation error because static main is not present. My suspicion is that I'm expecting the Solution Explorer to reflect what's going to be included/built. Although these files are visible in the project, it appears as though the build jumps over them. I will try my theory later and confirm in a separate post. – tentux Nov 15 '11 at 18:04
  • 3
    It appears to affect the build, which is all that ultimately matters, though the items stay visible in the solution explorer within the project, they are NOT included in the build process. Whether this is a disconnect between VS2010 and MSBUILD I'm not sure. – tentux Nov 15 '11 at 18:32
  • 2
    @tentux sorry for the late respones, but you are correct. Conditions on ItemGroups are evaluated at build time by the consuming target, thus you`ll still see everything in the Solution Explorer of the IDE. – Nick Nieslanik Nov 15 '11 at 19:48
2

I would like to extend the answer provided by Nick Nieslanik with some details just so others aren't stumped in the same way I was.

The solution works during build/publish, but Visual Studio 2010's interface may not reflect the changes made. Whether this is a defect or not, I am not sure, but it did confuse me and it may confuse others.

tentux
  • 253
  • 1
  • 2
  • 8