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

如何嵌入Razor类库生成的scoped css bundle文件?

解决方案

你当前配置执行时机过早的核心原因是:BundleScopedCssFiles目标仅负责收集待合并的scoped css文件列表,真正生成最终bundle文件的目标是GenerateScopedCssBundle,仅绑定前者作为后置触发条件会在bundle生成前就执行嵌入逻辑。

调整后的配置如下:

<Target Name="EmbedScopedCss" 
        AfterTargets="GenerateScopedCssBundle"
        BeforeTargets="BeforeResGen"
        Inputs="$(IntermediateOutputPath)scopedcss\bundle\$(AssemblyName).styles.css"
        Outputs="$(IntermediateOutputPath)EmbedScopedCss.stamp">
    <Message Importance="high" Text="嵌入scoped css bundle文件: $(IntermediateOutputPath)scopedcss\bundle\$(AssemblyName).styles.css" />
    <ItemGroup>
        <EmbeddedResource Include="$(IntermediateOutputPath)scopedcss\bundle\$(AssemblyName).styles.css" 
                          Type="Non-Resx" 
                          WithCulture="false" 
                          LogicalName="$(AssemblyName).styles.css" />
    </ItemGroup>
    <Touch Files="$(IntermediateOutputPath)EmbedScopedCss.stamp" AlwaysCreate="true" />
</Target>

配置说明

  • 调整AfterTargets为GenerateScopedCssBundle,确保bundle文件生成完成后才执行嵌入逻辑
  • 用$(AssemblyName)替代$(PackageId),适配未显式配置PackageId的项目,避免文件名不匹配
  • 新增LogicalName属性,嵌入后可直接通过文件名读取资源,无需拼接obj目录路径
  • 增加输入输出配置和戳文件,适配增量构建,避免重复执行无意义的嵌入操作
  • 新增高优先级日志输出,构建时可直接确认目标是否正常触发、路径是否正确

构建完成后可直接在生成的程序集资源中找到对应的css文件。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 10:48:03