.Net7.0 WebApp PublishSingleFile does not include wwwroot
Asked Answered
S

1

2

I have a .NET 7.0 web-app that I want to publish as a singlefile including all the content of its 'wwwroot' folder and the 'web.config'.

I am trying to publish with

dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true -p:IncludeAllContentForSelfExtract=true

and the following annotation in the .csproj:

<ItemGroup>
  <Content Update="wwwroot/**">
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <ExcludeFromSingleFile>false</ExcludeFromSingleFile>
  </Content>
</ItemGroup>

I use 'Update' instead of 'Include' because the files are already included (because of default=true of EnableDefaultContentItems)

After all this I still get a directory 'wwwroot' and the file 'web.config' in the publish directory. Peeking in the .exe i can see there are none of the files included.

Can someone help me publish this webapp as a singlefile?

I have made a console-test-project with files in a subfolder. In this test the configuration above includes the dummy files.

Science answered 13/1, 2023 at 9:31 Comment(0)
S
2

OK I found a solution: the publish-command was OK but the .csproject has to be modified further: first you have to exclude the desired folder from the DefaultItems it is part of (see pokes answer)

<PropertyGroup>
  <DefaultItemExcludes>wwwroot/**</DefaultItemExcludes>
  ...

Second, you have to re-include the directory as 'None' or in my case 'EmbeddedResource', but not 'Content' as I did above:

  <ItemGroup>
    <EmbeddedResource Include="wwwroot\**" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

Bonus: You can read your newly embedded-files as described here by adrianord on Jul 15, 2021

Science answered 13/1, 2023 at 14:16 Comment(2)
Sorry for being dense @BKNorr, but can you revise this for everything that is needed? I wasn't sure what from Poke's answer to include.Detergent
you only need the two bocks above in your <Project>; 1) in the the <PropertyGroup> you have to exclude the files/directories you want to include in your singlefile 2) you create a new Block <ItemGroup> in your <Project> that adds the files/Directories as an <EmbeddedResource >Science

© 2022 - 2024 — McMap. All rights reserved.