Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default
Asked Answered
B

25

180

Whenever I add a javascript or css file to my asp.net core project and I execute dotnet run in my bash terminal, I get the following error:

/usr/share/dotnet/sdk/1.0.1/Sdks/Microsoft.NET.Sdk/build/Microsoft

.NET.Sdk.DefaultItems.targets(188,5): error : Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultContentItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'wwwroot/css/BasicQuotation.css'; 'wwwroot/js/BasicQuotation.js' [/mnt/c/Dev/myproject/MyProject/MyProject.csproj]

The build failed. Please fix the build errors and run again.

I can fix this by removing the ItemGroup from my csproj file, but I don't think that's very productive.

This happens in the default Visual Studio 2017 ASP.NET Core Web Application (.NET Core) template. I add the files to my project by right clicking the wwwroot > js folder and then select Add > New Item > JavaScript File

This is my .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
  </PropertyGroup>

  <PropertyGroup>
    <UserSecretsId>aspnet-MyProject-7e1906d8-5dbd-469a-b237-d7a563081253</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="wwwroot\lib\jquery-validation\**" />
    <Content Remove="wwwroot\lib\jquery-validation\**" />
    <EmbeddedResource Remove="wwwroot\lib\jquery-validation\**" />
    <None Remove="wwwroot\lib\jquery-validation\**" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="wwwroot\css\BasicQuotation.css" />
    <Content Include="wwwroot\js\BasicQuotation.js" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" PrivateAssets="All" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Data\Commands\" />
    <Folder Include="Data\Queries\" />
    <Folder Include="wwwroot\images\" />
  </ItemGroup>

</Project>
Badmouth answered 10/4, 2017 at 14:30 Comment(3)
If you are running into this problem after updating VS2017 to v15.3, make sure you take a look here. MS decided to include certain folders such as wwwroot by default now, which means they probably are included twice.Badmouth
The article that @Badmouth shared, explains very well why this happens and how to fix it. Just go straight to that article.Mansoor
For more info on EnableDefaultContentItems: https://mcmap.net/q/137882/-what-content-items-does-lt-enabledefaultcontentitems-gt-enableNegative
M
129

So I ran into this same issue. I didn't want to turn off DefaultCompileItems because I knew that wouldn't "fix" the problem. So I unloaded my project and opened the .csproj file in text mode in Visual Studio and saw this.

<ItemGroup>
    <Content Include="wwwroot\css\custom-bootstrap-navbar.css" />
    <Content Include="wwwroot\images\friends-eating\image1.jpg" />
    <Content Include="wwwroot\images\friends-eating\image2.jpg" />
    <Content Include="wwwroot\images\friends-eating\image3.jpg" />
</ItemGroup>
<ItemGroup>
    <Folder Include="wwwroot\images\friends-eating\" />
</ItemGroup>

When I commented out the first ItemGroup block, it worked. What I assume is happening is that the project is adding the entire \images\friends-eating\ folder and then adding each individual image, causing a duplication.

As far as the custom css and js, the project automatically adds wwwroot\css and wwwroot\js so if you have an individual file added (like wwwroot\css\custom-bootstrap-navbar.css) it'll count as a duplicate.

Marthena answered 9/7, 2017 at 5:15 Comment(2)
This <Folder Include="wwwroot\" /> works for me, but visual studio undo that later on :(Sapiential
Ok so I don't get it. What the hell is visual studio actually doing and why?Nesline
P
228
1. Click 'Show All Files' in Solution Explorer
2. Right-click over 'wwwroot' select 'Exclude From Project'
3. Right-click over 'wwwroot' select 'Include in Project'
Physicality answered 17/8, 2017 at 7:46 Comment(10)
Doing this solved the problem for me, but what actually happened to make it work?Arithmetic
@Arithmetic Looks like it explicitly identifies all the files in your wwwroot file in the .csproj file. eg: i.imgur.com/4SPnQ5h.pngMastoiditis
Worked for me as well - and a far more safe way to go in my opinion than the accepted answer.Schurman
It worked for me too! Disabling DefaultContentItems helped build my solution, but images, CSSs and other files were not showing.Pam
Works! You might have to close and re-open the Solution Explorer in order to make the wwwroot folder visible again after step 2.Michelle
Once you exclude "wwwroot", you might have to click the "Show All Files" button (which looks like 3 sheets of paper messily stacked) at the top of the Solution Explorer to see the "wwwroot" folder so you can add it back in. Mine disappeared and I didn't know how to get it back until I found this button. The folder will look greyed out to indicate it's not included.Bough
On VS2019 for Mac, I had to do a "Remove (⌘+⌫)", making sure to not delete the files when asked, and then "Include in project", which worked perfectly!Lituus
Worked like a charm for me too. What I encountered was I had manually edited the .csproj file when I was publishing to IIS, to Include everything in wwwroot like so: <Folder Include="wwwroot**"> published my profile, had all css and js everything was ok. Next time I open my solution, project was unloaded, and could not reload because of duplicate content. Your solution is what worked for me.Asti
What this does is just remove the includes from the project file. So it is the same answer as the accepted answer, just with an different way to reach the same result.Uniformize
Great answer to an insane problem. I'm struggling to like dotnetcore at the momentVolt
M
129

So I ran into this same issue. I didn't want to turn off DefaultCompileItems because I knew that wouldn't "fix" the problem. So I unloaded my project and opened the .csproj file in text mode in Visual Studio and saw this.

<ItemGroup>
    <Content Include="wwwroot\css\custom-bootstrap-navbar.css" />
    <Content Include="wwwroot\images\friends-eating\image1.jpg" />
    <Content Include="wwwroot\images\friends-eating\image2.jpg" />
    <Content Include="wwwroot\images\friends-eating\image3.jpg" />
</ItemGroup>
<ItemGroup>
    <Folder Include="wwwroot\images\friends-eating\" />
</ItemGroup>

When I commented out the first ItemGroup block, it worked. What I assume is happening is that the project is adding the entire \images\friends-eating\ folder and then adding each individual image, causing a duplication.

As far as the custom css and js, the project automatically adds wwwroot\css and wwwroot\js so if you have an individual file added (like wwwroot\css\custom-bootstrap-navbar.css) it'll count as a duplicate.

Marthena answered 9/7, 2017 at 5:15 Comment(2)
This <Folder Include="wwwroot\" /> works for me, but visual studio undo that later on :(Sapiential
Ok so I don't get it. What the hell is visual studio actually doing and why?Nesline
K
36

This worked in my case:

 <PropertyGroup>
    ...
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
  </PropertyGroup>
Kleptomania answered 5/6, 2017 at 3:44 Comment(4)
As stated in my comment to Pinki's answer, I would not propose this as solution as this could lead to more errors.Volt
I actually ran into bigger problems setting this option to false. I would also recommend not setting it to false.Cathrine
What's the cleanest way to get a Folder to be included entirely as EmbeddedResource elements?Melatonin
After this wwwroot wasn't included any longer. So better don't use this...Gallon
F
17

As link says, you can disable this behavior (auto-include) and include all content explicitly by adding this into your csproj file:

<PropertyGroup>
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
</PropertyGroup>
Furgeson answered 10/4, 2017 at 15:18 Comment(4)
When I add that to my csproj file and I remove the css/js itemgroup, I get the following error: CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/mnt/c/Dev/myproject/MyProject/MyProject.csproj] The build failed. Please fix the build errors and run again.Badmouth
"Content" not "Compile" - Doesn't it?Kleptomania
One side note....if you don't explicitly include your Views folder, it won't publish your views with this settingLucerne
I tried this but it didn't work. still getting the same errorLawler
B
17

It happend when I upgrade my project from .NET Core 1.X to 2.0 just now. Here is my solution.

  • Open xxx.csproj, or right click project
  • Unload Project
  • Edit xxx.csproj.

Then remove ItemGroup items start with <Content Include = "wwwroot\xxxxx"

Burkley answered 15/8, 2017 at 10:1 Comment(1)
Thanks! Note: It seems to be related to latest Visual Studio version 15.3, not directly to .NET Core 2.0Mayst
P
10

My issues was close but not the exact same. My error was this:

C:\Program Files\dotnet\sdk\2.0.0-preview2-006497\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.targets(285,5): error : Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultContentItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'wwwroot\js\KOBindings.js'; 'wwwroot\js\KOPleaseWait.js'; 'wwwroot\js\ProjectTime\Add.js'; 'wwwroot\js\TimeAdmin\Invoice.js'; 'wwwroot\js\TimeAdmin\PayPeriodTotals.js' [C:\Avantia Projects\Time Card\avantia-timesheet\Solution\Almanac\Almanac.csproj]

If I did this:

<EnableDefaultContentItems>false</EnableDefaultContentItems>

It would not compile as all of the sudden Areas would not be recognized.

My solution, seems odd, but the message is telling me so, there were duplicate files:

The duplicate items were: 'wwwroot\js\KOBindings.js'; 'wwwroot\js\KOPleaseWait.js'; 'wwwroot\js\ProjectTime\Add.js'; 'wwwroot\js\TimeAdmin\Invoice.js'; 'wwwroot\js\TimeAdmin\PayPeriodTotals.js'

Looking at my .csproj file:

<ItemGroup>
  <Content Include="pdf.js" />
  <Content Include="wwwroot\js\package.json" />
  <Content Include="wwwroot\js\pdf.js" />
  <Content Include="wwwroot\js\KOBindings.js" />
  <Content Include="wwwroot\js\KOPleaseWait.js" />
  <Content Include="wwwroot\js\ProjectTime\Add.js" />
  <Content Include="wwwroot\js\TimeAdmin\Invoice.js" />
  <Content Include="wwwroot\js\TimeAdmin\PayPeriodTotals.js" />
</ItemGroup>

This was the ONLY location within the entire project where these files were references (aside from where they were loaded.) So the phrase duplicate does not make any sense to me. However, commenting those files out as such, took care of my problem:

<ItemGroup>
  <Content Include="pdf.js" />
  <Content Include="wwwroot\js\package.json" />
  <Content Include="wwwroot\js\pdf.js" />
  <!--
    <Content Include="wwwroot\js\KOBindings.js" />
    <Content Include="wwwroot\js\KOPleaseWait.js" />
    <Content Include="wwwroot\js\ProjectTime\Add.js" />
    <Content Include="wwwroot\js\TimeAdmin\Invoice.js" />
    <Content Include="wwwroot\js\TimeAdmin\PayPeriodTotals.js" />
  -->
</ItemGroup>

I assume this has something to do with the 2.0.0-preview2-006497 that I recently installed.

Also, this link mentions talks about globs. But does not tell me where that is. It talks about SDKs and such. Yet the answer was my custom .js files. That link needs to be updated or expanded on IMHO. Hope this helps someone.

Patrol answered 30/6, 2017 at 18:20 Comment(1)
Would love to know why this works. However, just worked for me. =)Federalist
T
4

.NET Core Projects

If you are in a class library, probably you'll need to remove all Compile/Content elements from your csproj as those are included automatically.

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <!-- NEEDED -->
  <ItemGroup>
    <ProjectReference />
    <ProjectReference />
  </ItemGroup>
  <!-- NOT NEEDED -->
  <ItemGroup>
   <Compile Include="Models\ExampleClass.cs" />
   <Content ... />
  </ItemGroup>
</Project>
Til answered 6/11, 2017 at 11:14 Comment(0)
F
4

So what worked for me was clicking on the file in question in the Solution Explorer and choosing "Exclude from Project". Then re-add it back using "Include in Project".

Ferriage answered 19/10, 2021 at 12:58 Comment(0)
M
3

Not that I can see it in your example above, to help other SO searchers..

You can also get this error when you have the same file listed twice in <Content Include="xxx" /> elements in your csproj file.

Remove the duplicate and rebuild.

Moneyer answered 11/4, 2017 at 7:50 Comment(0)
W
3

In my case, I solved this by deleting all files from the wwwroot-Directory in VS. Unload and reload the Project. Copy all files back in with VS. Done

Withstand answered 15/8, 2017 at 11:47 Comment(1)
None of the top solutions worked for me I did this before reading your comment and this works flawless!!! I excluded the wwwroot, controllers, views folders and added them back and now everything works fine!Faddist
D
3

Actually, Asp.net core automatically include content from wwwroot\css\ , wwwroot\js\ and wwwroot\lib\ location, so despite this if your csproj file explicitly include content from those directories then those content will be duplicated so removing content from you csproj file is the better way to get rid of this error. So remove below content-

  <ItemGroup>
    <Compile Remove="wwwroot\lib\jquery-validation\**" />
    <Content Remove="wwwroot\lib\jquery-validation\**" />
    <EmbeddedResource Remove="wwwroot\lib\jquery-validation\**" />
    <None Remove="wwwroot\lib\jquery-validation\**" />   
 </ItemGroup>

  <ItemGroup>
    <Content Include="wwwroot\css\BasicQuotation.css" />
    <Content Include="wwwroot\js\BasicQuotation.js" />   
  </ItemGroup>
Determined answered 15/8, 2017 at 12:34 Comment(3)
This happened to me on updating Visual Studio to 15.3. To prevent breaking changes to my colleagues who have not updated yet, I had to add <PropertyGroup> <EnableDefaultContentItems>false</EnableDefaultContentItems> </PropertyGroup> to the .csproj file.Lucerne
@Lucerne yes you are right, the same solution you can use in your context too. the choice is based on the developer whether he wants to keep EnableDefaultContentItems or not. They are two different approachDetermined
My point was that the only change I made was upgrading Visual Studio to 15.3. I made no changes to my project, but prior to the upgrade, this setting was not required.Lucerne
P
3

My case I is disable both below default items.

<EnableDefaultContentItems>false</EnableDefaultContentItems>
<EnableDefaultItems>false</EnableDefaultItems>
Parolee answered 13/11, 2018 at 3:0 Comment(0)
F
2

Under Visual Studio 2017 15.3, with .NET Core 2.0, EnableDefaultCompileItems did not work for me.

I needed to add this to my .csproj

  <PropertyGroup>
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
  </PropertyGroup>
Farceur answered 9/8, 2017 at 9:4 Comment(1)
For me (visual studio 15.3.4 .NET framework 4.5.2) EnableDefaultContentItems did not work while EnableDefaultCompileItems did.Hoofbeat
D
1

I found a different proper solution.

  1. Right click on your mvc project and click Edit csproj.
  2. If you are adding files under wwwroot, just move them to a folder under wwwroot, let's say it "theme"

And delete all content tags in csproj file if their exists, an example;

<Content Include="wwwroot\theme\favicon.ico" />
<Content Include="wwwroot\theme\fonts\cyrillic-ext400.woff2" />
<Content Include="wwwroot\theme\fonts\cyrillic-ext700.woff2" />

And only add this;

  <ItemGroup>
    <Folder Include="wwwroot\theme\" />
  </ItemGroup>

So, csproj file should be look like this;

    <Project Sdk="Microsoft.NET.Sdk.Web">

      <PropertyGroup>
        <TargetFramework>netcoreapp1.1</TargetFramework>
      </PropertyGroup>

      <PropertyGroup>
        <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
      </PropertyGroup>

      <ItemGroup>
        <Folder Include="wwwroot\theme\" />
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
...

With that way, I think you are adding all files under theme folder. So not include them one by one which occours some erros.

Deliberative answered 19/8, 2017 at 14:8 Comment(0)
F
1

All of the answers that were written before me did not resolve the 'Duplicate' errors for me, even though I had taken the due diligence to ensure their recommended procedures were followed.

For me, the 'Duplicate content' issue occurred when I tried to upgrade my WinUI 3 Application's SDK from Windows App SDK 0.8 to 1.0 in the Nuget Package Manager. When I did this, my project began to produce this error on every compile, and I tried to do the Exclude/Include methods that other suggested, but to no avail.

It took me about eight hours to find the culprit, which was a reference to another WinUI library project that I still had on Windows App SDK 0.8. Once I upgraded the other WinUI library project to SDK 1.0, this error went away.

Leaving this answer here in case it helps anyone else.

Fabrin answered 17/12, 2021 at 23:11 Comment(0)
V
1

We just had this error unexpectedly bring down our Azure DevOps pipeline for about 7 days. Although it's been well documented during the last 6 or so years, none of the obvious fixes e.g. EnableDefaultContentItems = False or removing duplicate <content> entries from our project file (we didn't have any) worked for us.

Our Azure DevOps pipeline uses the latest Windows image for building.

pool:
  vmImage: 'windows-latest'

We identified that image used for building had been automatically upgraded, which triggered the problem. The last working build showed in the Initialize job log:

Image: windows-2022
Version: 20231029.1.0

The first failing build showed:

Image: windows-2022
Version: 20231115.2.0

Something in the the new VS/MSBuild versions caused it to incorrectly detect duplicate content items.

Through the process of elimination, we found this evidently unneeded package reference in the *.csproj file was the culprit:

<PackageReference Include="Microsoft.NET.Sdk.Razor" Version="3.1.24" />

Removed this, and the problem went away.

Vulcanite answered 26/11, 2023 at 19:23 Comment(2)
This might be my problem. However, I initiated a VS 2022 update when it flashed on my screen thinking MS may have fixed the problem (since the problem occurred following a previous VS 2022 update).Bossuet
That did fix the problem. I do not know when or how that reference was added, but removing the reference to Razor fixed the problem.Bossuet
L
1

Helped me to remove this (old) line from my csProj-file:

''' < PackageReference Include="Microsoft.NET.Sdk.Razor" Version="3.1.32" / > '''

Lowgrade answered 8/12, 2023 at 11:42 Comment(0)
F
0

Excluding and Including back the folders that have duplicates error worked for me! Hope this helps someone else!

Faddist answered 16/8, 2017 at 19:11 Comment(0)
S
0

I think what disabling "EnableDefaultContentItems" isn't the best option. Manual cs-Proj file editing also isn't the good idea at all.

So for our build server pipeline, we wrote very small tool what will remove all duplicated entries automatically: dotnet-csproj-cleaner

We run it under Docker as the first build step in our continuous integration pipeline.

Separatist answered 17/8, 2017 at 10:15 Comment(2)
Seems nice but why is it limited to projects with a wwwroot path?Melatonin
It's standard VS content location and it's path included by default. If you need more complex solution - please feel free to submit pull request. It should be very easy by adding additional command line arguments. Thank you.Separatist
E
0

I had the same problem with only a file, and all others were working (all my templates were stored directly in wwwroot/content). The project was not created by me so I don't know many details.

The problem was fixed by renaming back and forth the file with the issue:

MyTemplate.html -- renamed --> MyTemplate2.html -- renamed --> MyTemplate.html

Note: At the first rename I got an error with something along the lines of "content configuration not found", but the second rename worked without issues.

After this I was able to compile the project successfully.

Emission answered 11/6, 2018 at 17:1 Comment(0)
B
0

I'm just getting started and I had this error when I tried copying a wwwroot folder from a Web Application template to a project created with an API template. removing bootstrap solved it for me, which is fine because I just need jquery.

Balky answered 30/4, 2021 at 21:41 Comment(0)
W
0

It happened to me on a new WinUI project in .Net 6 only when I referenced an old WinUI component. I solved it and many other unsolvable errors (InitalizeComponent not found for example) by doing the following on the old WinUI component :

  1. Remove the following packages

enter image description here

  1. Replace it with :

enter image description here

Worldling answered 7/7, 2022 at 22:47 Comment(0)
C
0

for .net core 6.0 this solved:

add to .csproj file

 <ItemGroup>
   <Content Update="wwwroot\**\*">
     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
   </Content>
 </ItemGroup>
Cy answered 9/8, 2022 at 12:0 Comment(0)
S
0

I had renamed one of my class files in Visual Studio for Mac which I could see the edits for when checking the git changes for the project file. Removing the file and adding it back to the project fixed the problem.

Superstratum answered 5/9, 2022 at 13:7 Comment(0)
R
0

add Global.json

{ "sdk": {"version" : "6.0.320"} }

Mention the target frame work that you need. to the root folder where the sln file is.

Rugg answered 23/11, 2023 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.