48

What does the Content Build Action in Visual Studio do? It does not look like it's doing anything.

The File Properties article on MSDN (does not exist anymore) says:

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.

But I have no idea what the "Content output group" means.

Is this something about deployment? Meaning, the action has no actual effect, when building, but only later when deploying?

0

4 Answers 4

35
+50

"Content" means that it is a deployable project item, it signals that the file needs to be copied to the target machine.

Something you can see with a simple console mode project. Project + Add New Item, pick the Bitmap File item template. Its Build Action is automatically set to "Content". Use Project + Properties, Publish tab and click the Application Files button. Note how the bitmap automatically got added to the list of deployed files:

enter image description here

Go back to the Properties window and change its Build Action to None. Click the button again and note how the file is now no longer included.

Also used by installer utilities that integrate with VS for the exact same reason. And a big deal in web applications, they usually have a lot of files that need to be deployed to the web server.

13

Updated Visual Studio documentation has more meaningful description:

Content - A file marked as Content can be retrieved as a stream by calling Application.GetContentStream. For ASP.NET projects, these files are included as part of the site when it's deployed.


Also after some testing and with a hint from What are the various "Build action" settings in Visual Studio project properties and what do they do?, I've found that the Content build action has this effect in WPF projects (possibly ASP too).

It adds

[assembly: System.Windows.Resources.AssemblyAssociatedContentFileAttribute("filename")]

to WpfApplication1_Content.g.cs. Read about the AssemblyAssociatedContentFileAttribute.

In console or WinForms applications, it does not do anything (neither in source code nor output binary).

Though in the comment, to previously mentioned question, there's a note about effect on deployment:

Also note that Content will be included when using one-click deploy, but None won't even if "copy if newer" is selected.

Possibly this works even for console and WinForms applications (I haven't tried).

4

I think this is only relevant for C# projects. See the image:

Content

If you set the 'Copy Output Directory' to 'Copy always' or 'Copy if newer' it will copy that file to the output folder when build. What action is performed when you press 'Build (solution)' depends on the 'Build action' property. From what I understand of the Microsoft website it just copies the file if it is a content file like a text, html or other normal file.

What other Build actions do is not known to me, it seems like an advanced option.

The bottom line: it seems a feature that helps you keep the folder structure and files organised each build, so it is always ready for deployment. I hope this gives you an idea of the feature.

5
  • Thanks for your answer. I believe that when building, the file is copied to output directory based on Copy to output directory property only. It does not depend on Build action (it's copied even when the action is kept to default None). It also does not seem to depend of file type. Any file type, even obviously non-content types (like .exe) are copied. So I don't think this answers my question. Commented Mar 26, 2014 at 16:55
  • 2
    @Martin Prikryl That is the intent of the Content type. If you want to compile it into the executable you will have to use a different type. You can also use the Embedded Resource type which will compile the file into the executable.
    – RB-Develop
    Commented Mar 26, 2014 at 18:03
  • I do not want to compile anything into executable. I'm simply asking what effect has if I change Build action from None to Content. Commented Mar 26, 2014 at 18:24
  • 1
    I think it just marks the file for a different kind of processing, as the website says: none is for files like a readme that are not part of the program itself. Content will probably mark it as part of the program and stores a reference somewhere. This is - at the moment - the best I can come up with, if it is not satisfactory I still hope it helps out in some way :-).
    – RB-Develop
    Commented Mar 26, 2014 at 20:13
  • Thanks for your effort, but no, it's not satisfactory :) I did not find any kind of reference when set to Content. Commented Mar 26, 2014 at 20:18
0

When publishing to an Azure web site with "Precompile during publishing" checked, views that were Build Action "None" failed to resolve in the browser with an precompile error. Once I flipped the Build Action to "Content" and published, the views resolved as expected. Not sure how 2 of 100 views got set to "None".

Not the answer you're looking for? Browse other questions tagged or ask your own question.