If you have an ASP.NET Core project that was working fine and you upgrade to Visual Studio 2017, you're bound to hit the Duplicate Content compile error. This will affect you regardless of which framework you use and affects both .NET Framework and .NET Core projects, even if you haven't upgraded to .NET Core 2.0.
In this post, I explain why it happens, why the change has been made, and the solutions you can use to fix it.
If you get a duplicate content compile error after upgrading to Visual Studio 2017 v15.3 on your ASP.NET Core projects. Right click on the project in solution explorer, select Edit [YourProjectName].csproj, then remove the lines that include content, such as those including content in wwwroot.
A Common Problem
I upgraded Visual Studio 2017 to v15.3 and as a first step, tried to compile some existing projects to make sure everything was working. Unfortunately, I was greeted by this 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.
And it seems like everyone else is running into the same issue. Robin Clark was hit by it and worked around it by removing and then re-adding the duplicate items.
I've encountered people asking about the duplicate content error on Twitter.
And I've been asked about it by others too. So, it is something you're likely to encounter when upgrading to Visual Studio 2017 v15.3.
Visual Studio 2017 v15.3 has added new default includes that add certain content to your project automatically. The problem is, your existing projects already import these files manually.
This means that your content, such as the files in wwwroot are now getting imported by the default includes that are built-in to Visual Studio and by the manual rules in your csproj file. Therefore, you're importing them twice and you get the duplicate content error.
A Great Change
Despite causing this error and a little bit of work, this is a great change, because it means you no longer have to worry about including these files manually in your csproj file.
Prior to this change, you needed to manually include content files like this:
<ItemGroup> <Content Include="wwwroot\styles\default.css" /> <Content Include="wwwroot\scripts\default.js" /> </ItemGroup>
Or you had to write a file globbing rule like this:
<ItemGroup> <Content Include="wwwroot\**" /> </ItemGroup>
When Visual Studio 2017 was first released, many of my projects were using the manual style and this often caused issues when adding and removing files, as the includes would get out-of-sync or I ended up with duplicate entries. I changed to the globbing rule style to fix these issues.
This new Visual Studio feature removes the need for any rules and provides the globbing style rule by default. The only problem, is that for existing projects, you need to fix the duplicate content error.
The solution is simple. Right click on the affected project in solution explorer, select Edit [YourProjectName].csproj, then remove the lines that specify what content to include. This is going to be lines like the ones that include content in wwwroot. I feel this is the best solution as you can see all the included content in one place and easily remove them all in one step.
There are other workarounds. You can remove the duplicate content and then add it again as Robin did. Adding the items one at a time is tedious, but you could show all files and then include them all in one go.
The compile error itself suggests another solution. You can disable the default compile items. You do that by setting the EnableDefaultCompileItems property to false:
<PropertyGroup> <EnableDefaultCompileItems>false</EnableDefaultCompileItems> </PropertyGroup>
I don't recommend using this last solution, as it means that you'll need to maintain your content includes going forward. However, if you need to have precise control over what files are included, then you can use this to revert to the old manual style with full control.