最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Embedding wwwroot Directory as Embedded Resource in Blazor Hybrid WinForms App for Single-File Publish - Stack Overflow

programmeradmin0浏览0评论

I have a WinForms app using Blazor Hybrid, which generates a wwwroot directory containing static files (JS, CSS, fonts). When publishing the app as a single-file executable, I want to hide the wwwroot directory and embed its content as resources. I plan to use EmbeddedFileProvider to consume these resources at runtime.

Here’s my current approach, but it doesn’t seem to embed the files:

<PropertyGroup>
  <EmbedWwwroot>true</EmbedWwwroot>
  <PublishWwwroot>$(PublishDir)wwwroot</PublishWwwroot>
</PropertyGroup>

<Target Name="EmbedPublishedWwwroot" AfterTargets="Publish" Condition="$(EmbedWwwroot) == 'true'">
  <ItemGroup>
    <PublishedWwwrootFiles Include="$(PublishWwwroot)\**\*.*" />
  </ItemGroup>
  <Message Text="Published wwwroot path: $(PublishWwwroot)" Importance="high" />
  <ItemGroup>
    <EmbeddedResource Include="@(PublishedWwwrootFiles)">
      <LogicalName>wwwroot.$([MSBuild]::ValueOrDefault('%(RecursiveDir)', '').Replace('\', '.'))%(Filename)%(Extension)</LogicalName>
      <Visible>false</Visible>
      <Type>Non-Resx</Type>
    </EmbeddedResource>
  </ItemGroup>
  <Message Text="Embedding wwwroot content: %(EmbeddedResource.Identity) - %(EmbeddedResource.LogicalName)" Importance="high" />
</Target>

<Target Name="RemoveWwwrootFromPublish" AfterTargets="Publish" Condition="$(EmbedWwwroot) == 'true'">
  <ItemGroup>
    <RemoveDir Directories="$(PublishWwwroot)" Condition="Exists($(PublishWwwroot))" />
  </ItemGroup>
</Target>

The wwwroot files are not being embedded as expected. I suspect this is because the embedding logic runs in the AfterTargets="Publish" phase, but the wwwroot content is only available during publish targets, and embedding typically needs to happen during the core compile phase.

How can I properly embed the wwwroot content as resources during the correct build phase? Additionally, how can I use EmbeddedFileProvider to load these resources at runtime in a Blazor Hybrid app? Any guidance or corrections would be greatly appreciated!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论