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

.NET 9 Web项目如何禁用静态文件指纹功能?

解决.NET 9中Photino.Blazor静态文件指纹导致的编译错误

直接解决方案

在你的Photino.Blazor项目的.csproj文件中,添加以下属性配置,彻底禁用.NET 9新增的静态资源指纹功能:

<PropertyGroup>
  <StaticWebAssetsEnableFingerprinting>false</StaticWebAssetsEnableFingerprinting>
</PropertyGroup>

问题背景

你之前为了修复_content目录在编译/发布时的异常表现,自定义了_StaticWebAssetsComputeFilesToBuild目标:

<Target Name="_StaticWebAssetsComputeFilesToBuild" AfterTargets="GetCopyToOutputDirectoryItems" DependsOnTargets="ResolveStaticWebAssetsInputs">
  <ItemGroup>
      <!-- Filter the static web assets foreign to the project and then add them to the list of resolved
           files to publish.
       -->
      <_ExternalBuildStaticWebAsset Include="%(StaticWebAsset.FullPath)" Condition="'%(SourceType)' != ''">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <TargetPath>$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)','$([MSBuild]::NormalizePath('wwwroot\%(BasePath)\%(RelativePath)'))'))</TargetPath>
      </_ExternalBuildStaticWebAsset>
      <!-- Remove any existing external static web asset that might have been added as part of the
           regular publish pipeline. -->
      <_SourceItemsToCopyToOutputDirectory Remove="@(_ExternalBuildStaticWebAsset)" />
      <_SourceItemsToCopyToOutputDirectory Include="@(_ExternalBuildStaticWebAsset)" />
  </ItemGroup>
</Target>

该配置在.NET 8环境下运行正常,但安装.NET 9 SDK后(即便项目目标框架仍是.NET 8),执行dotnet build the.csproj时会触发如下错误:

13>Microsoft.Common.CurrentVersion.targets(5322,5): Error MSB3021 : 无法将文件"E:\Code\open-source\REDACTED\wwwroot\webfonts\quicksand\Quicksand-SemiBold.ttf"复制到"bin\Debug\net8.0\wwwroot_content\REDACTED\webfonts\quicksand\Quicksand-SemiBold#[.{fingerprint}]?.ttf"。文件名、目录名或卷标语法不正确。: 'E:\Code\open-source\REDACTED\bin\Debug\net8.0\wwwroot_content\REDACTED\webfonts\quicksand\Quicksand-SemiBold#[.{fingerprint}]?.ttf'。

这是因为.NET 9默认启用了静态文件指纹功能,会自动给静态资源文件名添加#[.{fingerprint}]?后缀,和你自定义的目标处理逻辑冲突,导致文件路径包含非法字符,最终复制失败。

额外说明

禁用指纹功能后,你原来的自定义目标可以继续正常工作,同时也能平滑升级到.NET 9框架,无需担心静态文件被额外处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 06:54:51