首页 文章

包含了重复的'Content'项目 . 默认情况下,.NET SDK包含项目目录中的'Content'项

提问于
浏览
92

每当我向我的asp.net核心项目添加javascript或css文件并在我的bash终端中执行 dotnet run 时,我收到以下错误:

/usr/share/dotnet/sdk/1.0.1/Sdks/Microsoft.NET.Sdk/build/Microsoft .NET.Sdk.DefaultItems.targets(188,5):错误:包含重复的“内容”项目 . .NET SDK默认包含项目目录中的“内容”项 . 您可以从项目文件中删除这些项,或者如果要在项目文件中明确包含它们,请将“EnableDefaultContentItems”属性设置为“false” . 有关更多信息,请参阅https://aka.ms/sdkimplicititems . 重复的项目是:'wwwroot / css / BasicQuotation.css'; 'wwwroot / js / BasicQuotation.js'[/mnt/c/Dev/myproject/MyProject/MyProject.csproj]构建失败 . 请修复构建错误并再次运行 .

我可以通过从我的csproj文件中删除 ItemGroup 来解决这个问题,但我的工作效率很高 .

这在默认的Visual Studio 2017 ASP.NET核心Web应用程序(.NET Core)模板中发生 . 我通过右键单击wwwroot> js文件夹将文件添加到我的项目中,然后选择 Add > New Item > JavaScript File

这是我的.csproj文件:

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

16 回答

  • 0

    当我将项目从.NET Core 1.X升级到2.0时,就会发生这种情况 . 这是我的解决方案 .

    • 打开xxx.csproj,或右键单击项目

    • 卸载项目

    • 编辑xxx.csproj .

    然后删除以 <Content Include = "wwwroot\xxxxx" 开头的ItemGroup项目

  • 1

    就我而言,我通过删除VS中wwwroot-Directory中的所有文件解决了这个问题 . 卸载并重新加载项目 . 使用VS复制所有文件 . 完成

  • 1

    所以我遇到了同样的问题 . 我不想关闭DefaultCompileItems,因为我知道这不会“解决”问题 . 所以我卸载了我的项目并在Visual Studio中以文本模式打开了.csproj文件并看到了这一点 .

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

    当我注释掉第一个ItemGroup块时,它起作用了 . 我假设发生的是该项目正在添加整个\ images \ friends-eating \文件夹,然后添加每个单独的图像,导致重复 .

    至于自定义css和js,项目会自动添加wwwroot \ css和wwwroot \ js,所以如果你添加了一个单独的文件(如wwwroot \ css \ custom-bootstrap-navbar.css),它将被视为重复 .

  • 21

    实际上,Asp.net核心自动包含来自 wwwroot\css\wwwroot\js\wwwroot\lib\ 位置的内容,所以尽管如此,如果您的 csproj 文件明确包含来自这些目录的内容,那么这些内容将被复制,因此从您删除内容 csproj 文件是更好的方法摆脱这个错误 . 所以删除以下内容 -

    <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>
    
  • 12

    link says,您可以通过将此行添加到 csproj 文件中来禁用此行为(自动包含)并显式包含所有内容:

    <PropertyGroup>
        <EnableDefaultContentItems>false</EnableDefaultContentItems>
    </PropertyGroup>
    
  • 0

    排除并包含有重复错误的文件夹对我有效!希望这有助于其他人!

  • 91

    在Visual Studio 2017 15.3下,使用.NET Core 2.0,EnableDefaultCompileItems对我不起作用 .

    我需要将它添加到我的.csproj中

    <PropertyGroup>
        <EnableDefaultContentItems>false</EnableDefaultContentItems>
      </PropertyGroup>
    
  • 0

    我只有一个文件有同样的问题,所有其他人都在工作(我的所有模板都直接存储在wwwroot / content中) . 该项目不是由我创建的,所以我不知道很多细节 .

    该问题由 renaming back and forth 修复了问题:

    MyTemplate.html - 重命名 - > MyTemplate2.html - 重命名 - > MyTemplate.html

    Note :在第一次重命名时,我遇到了“内容配置未找到”的错误,但第二次重命名没有问题 .

    在此之后,我能够成功编译项目 .

  • 10

    我的情况是我在默认项目下禁用机器人 .

    <EnableDefaultContentItems>false</EnableDefaultContentItems>
    <EnableDefaultItems>false</EnableDefaultItems>
    
  • 6

    并非我能在上面的例子中看到它,以帮助其他SO搜索者 .

    如果在 csproj 文件的 <Content Include="xxx" /> 元素中列出了两次相同的文件,也会出现此错误 .

    删除副本并重建 .

  • 0

    我认为什么禁用"EnableDefaultContentItems"不是最好的选择 . 手动 cs-Proj 文件编辑也不是一个好主意 .

    因此,对于我们的构建服务器管道,我们编写了一个非常小的工具,将自动 remove 所有重复的条目:dotnet-csproj-cleaner

    我们在Docker下运行它作为我们持续集成管道中的第一个构建步骤 .

  • 0

    我的问题很接近但不完全相同 . 我的错误是这样的:

    C:\ Program Files \ dotnet \ sdk \ 2.0.0-preview2-006497 \ Sdks \ Microsoft.NET.Sdk \ build \ Microsoft.NET.Sdk.DefaultItems.targets(285,5):错误:重复'内容'物品包括在内 . .NET SDK默认包含项目目录中的“内容”项 . 您可以从项目文件中删除这些项,或者如果要在项目文件中明确包含它们,请将“EnableDefaultContentItems”属性设置为“false” . 有关更多信息,请参阅https://aka.ms/sdkimplicititems . 重复的项目是:'的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]

    如果我这样做:

    <EnableDefaultContentItems>false</EnableDefaultContentItems>
    

    它不会编译,因为所有突然 Areas 将无法识别 .

    我的解决方案,似乎很奇怪,但消息告诉我,有重复的文件:

    重复的项目是:'wwwroot \ js \ KOBindings.js'; '的wwwroot \ JS \ KOPleaseWait.js'; '的wwwroot \ JS \ ProjectTime \ Add.js'; '的wwwroot \ JS \ TimeAdmin \ Invoice.js'; '的wwwroot \ JS \ TimeAdmin \ PayPeriodTotals.js'

    看着我的 .csproj 文件:

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

    这是整个项目中唯一的位置,这些文件是引用的(除了它们被加载的位置 . )因此,复制短语对我没有任何意义 . 但是,将这些文件注释掉,照顾我的问题:

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

    我认为这与我最近安装的 2.0.0-preview2-006497 有关 .

    此外,这提到了关于globs的讨论 . 但是不告诉我它在哪里 . 它讨论了SDK等 . 然而答案是我的自定义 .js 文件 . 需要在恕我直言上更新或扩展该链接 . 希望这有助于某人 .

  • 2

    .NET Core Projects

    如果您在类库中,可能需要从csproj中删除所有编译/内容元素,因为这些元素是自动包含的 .

    <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>
    
  • 2

    这适用于我的情况:

    <PropertyGroup>
        ...
        <EnableDefaultContentItems>false</EnableDefaultContentItems>
      </PropertyGroup>
    
  • 1
    • 在解决方案资源管理器中单击'Show All Files'

    • 右键单击'wwwroot'选择'Exclude From Project'

    • 右键单击'wwwroot'选择'Include in Project'

  • 159

    我找到了一个不同的正确解决方

    • 右键单击您的mvc项目,然后单击编辑csproj .

    • 如果您要在wwwroot下添加文件,只需将它们移动到wwwroot下的文件夹,让我们说吧"theme"

    并删除csproj文件中的所有内容标签(如果存在),例如;

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

    并且只添加这个;

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

    所以,csproj文件应该是这样的;

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

    通过这种方式,我认为您正在添加主题文件夹下的所有文件 . 所以不要逐个包含它们,这会产生一些错误 .

相关问题