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

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

.NET.Sdk.DefaultItems.Targets(188,5):错误:重复的‘Content’ 物品都包括在内..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来解决这个问题,但我认为这不是很有效.

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

这是我的.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>

推荐答案

所以我遇到了同样的问题.我不想关闭DefaultCompileItems,因为我知道这不会"解决"问题.所以我卸载了我的项目并打开了.在VisualStudio中,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),它将被视为重复文件.

Asp.net相关问答推荐

如何在 asp.net 服务器中使用字体

RNGCryptoServiceProvider - 随机数审核

从 Request.Url.AbsolutePath 获取应用相对 url

将 ASP.NET 成员资格表添加到我自己的现有数据库中,还是应该配置一个单独的 ASP.NET 成员资格数据库?

在视图 Mvc.net 中访问路由值

无法使用单例Microsoft.AspNetCore.Hosting.Internal.HostedServiceExecutor中的范围服务MyDbContext

可空日期时间转换

Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用

如何从 SQL Server 2008 本身获取客户端 IP 地址?

如何找出我的 Windows 域上托管 LDAP 的服务器?

如何确定服务器端 C# 上的浏览​​器窗口大小

HttpContext.Current 在 MVC 4 项目中未解决

如何从 ASP.NET 中的数据表/数据视图中 Select 前 n 行

脚本管理器控制实际上是做什么的?

验证请求事件

ASP.NET MVC 和 httpRuntime executionTimeout

ASP.net 判断页面是 http 还是 https

在 ASP.NET Web API 2 中禁用 *all* 异常处理(为我自己腾出空间)?

RegisterStartupScript 不适用于 ScriptManager、Updatepanel.这是为什么?

MVC4 中 Global.asax.cs 页面中的问题