160

How is

<None Include="C:\foo.bar" />

different from

<Content Include="C:\foo.bar" />

?

Emmett
  • 13,097
  • 12
  • 52
  • 78
  • 7
    I had a few `Content`s get changed to `None`s. I think it happened when I renamed files from .ascx to .cshtml when converting to Razor. Changing them back manually fixed some deployment issues I had. Glad I found this. – Chris Feb 24 '11 at 18:13

8 Answers8

148

The MSDN article on the build action property explains the differences.

None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.

Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.

Community
  • 1
  • 1
adrianbanks
  • 76,725
  • 21
  • 166
  • 198
  • 5
    Thanks, I had trouble locating this reference. – Emmett Jun 29 '09 at 22:26
  • Helpful, but why have a "None" item at all, if the file is not to be included in the output? What's the point? – JimH44 Apr 23 '18 at 09:03
  • 1
    @JimH44 items are often used to specifically copy files into the output directory, even though they're not in the output group *by default*. If you select "Copy to Output Directory" -> "Copy Always" for a file, this will cause it to be copied, and you will see the entry added to the project's .csproj file will have a entry for the file. – Conspicuous Compiler Feb 26 '19 at 16:39
  • 7
    So then what is difference between a file with "Copy to Output Directory" -> "Copy Always" and a file with "Copy to Output Directory" -> "Copy Always"? – hashtable Mar 25 '20 at 01:57
  • @JimH44 I have some SQL files that I execute in a post-deployment process. Don't need them to be part of the build, nor to be included in the output. I believe there are plenty of such use-cases for this approach. – dvlsc Dec 18 '20 at 09:30
32

One difference is how they get published; "None" items don't get included in a publish, "Content" items do; for example, on the "Application Files" dialog on the Publish tab.

Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 13
    Not true. At least in the new dot net cli. if you have an item declared as None with a `CopyToOutputDirectory` set to `true` it will get published in a `dotnet publish` – disklosr Jan 03 '19 at 10:14
13

I am not 100% sure (I read the MSDN description of Build Action property) but just copying that answer from MSDN to StackOverflow does not answer the question completely for me.

The difference of None and Content only has an effect on Web projects. For a command line project, WinForm project or UnitTest project (in my case) etc. None and Content have no different behavior.

MSDN: "project output group" or "Content output group" only terms used in a Web project, right?

hfrmobile
  • 913
  • 12
  • 15
  • 2
    Like you say, seems `None` vs `Content` only has effect for the _publish_ step not the _build_ step. For the build step seems `CopyToOutputDirectory` can be set on either and that controls if the file is copied by the build step. Also seems odd that by default T4 templates are added as `Content` items and not `None` items. – Christopher King Oct 02 '16 at 22:14
8

In my situation, my MSBuild file had an ItemGroup for image resources that appeared as follows:

  <ItemGroup>
    <Content Include="Resources\image001.png" />
    <Content Include="Resources\image002.png" />
    <Content Include="Resources\image003.png" />
    <Content Include="Resources\image004.png" />
    <None Include="Resources\image005.png" />
    <None Include="Resources\image006.png" />
    <None Include="Resources\image007.png" />
  </ItemGroup>

While my project was building fine, this left me wondering why I had a mix of Content and None item type elements in my ItemGroup. This MSDN article (for Visual Studio 2010) gave me the guidance I was looking for:

Note that when the resource editor adds an image, it sets Build Action to None, because the .resx file references the image file. At build time, the image is pulled into the .resources file created out of the .resx file. The image can then easily be accessed by way of the strongly-typed class auto-generated for the .resx file. Therefore, you should not change this setting to Embedded Resource, because doing this would include the image two times in the assembly.

Resolution: With this guidance, using a text editor, I changed the Content item type elements to None.

Also, for an overview of MSBuild items, see this MSDN article.

DavidRR
  • 15,000
  • 17
  • 89
  • 169
3

I have a project that contains no compilable items (it stores html and javascript for jasmine unit tests).

One day my solution (that contained said project) stopped compiling saying "The target "Build" does not exist in the project".

I added an import to bring in the compiler, which worked fine on my machine but failed using msbuild on the build server.

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

I then changed a line from

<None Include="SpecRunner.html" />

to

<Content Include="SpecRunner.html" />

and it worked on the build server as well.

cedd
  • 1,561
  • 1
  • 16
  • 33
2

You need None in a template project file to include files you define in the .vstemplate otherwise they are lost in the creation & translation process. They get left behind in the temp folder it uses to build everything and then deleted shortly after.

CAD bloke
  • 7,546
  • 6
  • 58
  • 104
2

In my case .Pubxml is one of those files among None list. It's not meant for solution building or as a static file for web project. But to publish the site to Azure, the configurations are present in this.

As per Microsoft article these are the major types we see among .csproj file tags:

None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.

Compile - The file is compiled into the build output. This setting is used for code files.

Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.

Embedded Resource - This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.

IKriKan
  • 792
  • 10
  • 11
  • 1
    Please comment here, if something is misleading or not clear, Just a down vote doesn't help me improve it any better. Thanks in advance. – IKriKan Jan 07 '19 at 04:34
  • 1
    I often got error: "None" element name for include "Properties\PublishProfiles\FolderProfile.pubxml" should be "Content". in my Asp.Net project on Bamboo, and sometime rerun the build can just fix it, so don't know what exactly the problem is – Bochen Lin Jun 09 '20 at 07:02
  • Hi @BochenLin, the details you mentioned here are bit scanty, can you try to give more details here or post a new question with complete issue details and screenshot if possible [and then share the question link here] . – IKriKan Jun 09 '20 at 11:01
2

Content files are not included in a build, but are included in a publish.

None files are not included in a build or publish, unless they are configured that way by you. For instance, a "Copy to Output Directory" setting of "Always" or "Newer", will cause them to be included in both a build and publish.

carlin.scott
  • 3,719
  • 2
  • 19
  • 27