You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 9 Blazor中DefineStaticWebAssetEndpoints任务执行异常求助

解决.NET 9/Visual Studio 9中"DefineStaticWebAssetEndpoints"任务重复键异常的问题

升级到.NET 9和Visual Studio 9后,出现以下构建错误:

"DefineStaticWebAssetEndpoints" task failed unexpectedly.
System.ArgumentException: An item with the same key has already been added.

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer`1 comparer)
at Microsoft.AspNetCore.StaticWebAssets.Tasks.DefineStaticWebAssetEndpoints.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()

已尝试的排查步骤:

  • 检查.csproj中是否存在同一文件的重复引用项
  • 清理构建缓存后重新运行项目
  • 重启Visual Studio

以下是补充解决方案:

额外排查与修复步骤

  • 检查依赖包中的重复静态资源
    部分NuGet包可能会引入重复的静态资源端点。展开项目的「依赖项」节点,排查是否有重复引用的类库(尤其是包含JS、CSS、图片等静态资源的包),尝试暂时移除可疑包后重新构建,定位问题来源。

  • 清理全局缓存与依赖
    执行以下命令清理本地及全局的.NET缓存和NuGet资源:

    dotnet clean
    dotnet nuget locals all --clear
    

    之后重新恢复依赖并构建项目:

    dotnet restore
    dotnet build
    
  • 检查配置文件中的重复项
    打开launchSettings.json和appsettings.json,确认StaticWebAssets相关节点下是否存在重复的路径映射配置,确保每个资源键唯一。

  • 修正项目文件的静态资产配置
    若项目.csproj中显式配置了<StaticWebAssets>节点,检查是否有重复的Include或Content项,比如:

    <ItemGroup>
      <StaticWebAssets Include="wwwroot/css/site.css" />
      <!-- 避免重复添加同一路径的资源 -->
    </ItemGroup>
    
  • 升级到最新版本
    该异常可能是.NET 9早期版本的已知问题,通过Visual Studio的「检查更新」功能升级至最新版本,或执行命令:

    dotnet update
    

内容的提问来源于stack exchange,提问作者Lakindu Kariyawasam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:05:56