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

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

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:

netcoreapp1.1$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;aspnet-MyProject-7e1906d8-5dbd-469a-b237-d7a563081253

回答1:

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.

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.



回答2:

  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'


回答3:

This worked in my case:

      ...     false


回答4:

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



回答5:

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:

false

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:

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:

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.



回答6:

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

false


回答7:

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



回答8:

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-

  


回答9:

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 elements in your csproj file.

Remove the duplicate and rebuild.



回答10:

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

  false


回答11:

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;

And only add this;

  

So, csproj file should be look like this;

    netcoreapp1.1$(PackageTargetFallback);portable-net45+win8+wp8+wpa81; ... 

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



回答12:

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



回答13:

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.



回答14:

.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.

netcoreapp2.0


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!