Home > Enterprise >  .Net7.0 WebApp PublishSingleFile does not include wwwroot
.Net7.0 WebApp PublishSingleFile does not include wwwroot

Time:01-16

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.

CodePudding user response:

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

  • Related