tbrownaw
tbrownaw

Reputation: 193

What content items does <EnableDefaultContentItems> enable?

I have an asp.net core 1.1 app.

The .csproj for it has an entry

<EnableDefaultContentItems>false</EnableDefaultContentItems>

When I search for this online, all I find is questions about duplicate content errors. What are the default items being enabled (or rather, not enabled) here? And, has Microsoft documented this somewhere that I should know to look?

Upvotes: 18

Views: 13849

Answers (1)

poke
poke

Reputation: 388153

This is part of the new project format, in particular the new Microsoft.NET.Sdk.Web project SDK that is being used for ASP.NET Core projects.

By default, EnableDefaultContentItems is set to true. The SDK’s MSBuild property project then contains the following:

<ItemGroup Condition=" '$(EnableDefaultItems)' == 'true' And '$(EnableDefaultContentItems)' == 'true' ">
  <!-- Publish everything under wwwroot, all JSON files, all web.config files and all Razor files -->
  <Content Include="wwwroot/**" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  <Content Include="**/web.config" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);wwwroot/**" />
  <Content Include="**/*.cshtml" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);wwwroot/**" />
  <Content Include="**/*.json" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);wwwroot/**" />

  <!-- Set CopyToPublishDirectory to Never for items under AppDesignerFolder ("Properties", by default) to avoid publishing launchSettings.json -->
  <Content Update="$(AppDesignerFolder)/**" CopyToPublishDirectory="Never" Condition="'$(AppDesignerFolder)' != ''"/>

  <!-- Remove Content items from other item types (in a way that CPS understands) -->
  <None Remove="wwwroot/**;**/*.json;**/web.config;**/*.cshtml" />
  <Compile Remove="wwwroot/**" />
  <EmbeddedResource Remove="wwwroot/**" />

  <!-- Keep track of the default content items for later to distinguish them from newly generated content items -->
  <_ContentIncludedByDefault Include="@(Content)" />

</ItemGroup>

So basically, EnableDefaultContentItems makes the project automatically:

  • Publish all files in wwwroot/, any web.config and all .cshtml and .json files.
  • Ignore the Properties/ folder on publish
  • Prevent those published content files from being compiled or embedded.

So if you are using the wwwroot folder and haven’t changed its name, then it’s advised to just keep the default to avoid having to specify all these exceptions manually within your project. These are just the common defaults that allow you to get your project running quickly without MSBuild getting in your way.

Of course, just because these are the defaults, you could still always have more explicit rules later for individual paths, without having to disable the default content items.

Upvotes: 25

Related Questions