2

EDIT

For info, I'm developping on macOS using VS Code

I'm trying to include files in my publish process ( Currently cshtmlthat represents my email templates ).

I follow this thread on github but seems that their solutions don't work for me.

Here my csproj to add an unique cshtml file :

  <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <ItemGroup>
      <EmailFile Include="$(ProjectDir)/EmailTemplates/OrderCompleteEmail.cshtml" />
    </ItemGroup>
    <Copy SourceFiles="@(EmailFile)" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
  </Target>
OrcusZ
  • 3,555
  • 2
  • 31
  • 48
  • look for msbuild info. EmailFile is not a default variable/property in msbuild. Check this question links https://stackoverflow.com/questions/1452962/list-of-msbuild-built-in-variables – Cleptus Mar 23 '18 at 07:51
  • 1
    Have you tried to set the file to copy to the output folder? Right click -> Properties -> Build Action = None, Copy To Output Directory = Copy If Newer. When I try this with a text file it is published along with everything else – Simply Ged Mar 23 '18 at 08:34
  • why don't use [CopyToPublishDirectory](https://stackoverflow.com/a/42713770/2833802) property? – Set Mar 23 '18 at 08:59
  • @Set I already tried CopyToPublishDirectory without sucess. – OrcusZ Mar 23 '18 at 09:10
  • @bradbury ok. Thinking that we can create nodes ^^' – OrcusZ Mar 23 '18 at 09:11

1 Answers1

9

Your solution was almost correct, you have to use AfterTargets="Publish":

<Target Name="CopyCustomContentOnPublish" AfterTargets="Publish">
  <ItemGroup>
    <EmailFile Include="EmailTemplates/OrderCompleteEmail.cshtml" />
  </ItemGroup>
  <Copy SourceFiles="@(EmailFile)" DestinationFolder="$(PublishDir)" />
</Target>

You can also copy all your email templates in a single Target to the same folder like:

  <Target Name="CopyCustomContentOnPublish" AfterTargets="Publish">
    <ItemGroup>
      <EmailTemplates Include="EmailTemplates\*.cshtml" />
    </ItemGroup>
    <Copy SourceFiles="@(EmailTemplates)" DestinationFolder="$(PublishDir)%(EmailTemplates.RelativeDir)" />
  </Target>
Dominik
  • 649
  • 7
  • 16
  • Yes I did it like that some times ago ^^ – OrcusZ Jul 03 '18 at 07:28
  • 1
    Nice, solution, though be careful, there is a strange discrapancy between dotnet publish and VS 2017 (15.8.4) Publish - VS won't work with `AfterTargets="AfterPublish"` but will with `AfterTargets="Publish"`. The later works with both - VS and dotnet publish. – the berserker Oct 08 '18 at 09:27
  • Thank you for you comment @theberserker, I updated my answer based on it. – Dominik Nov 09 '18 at 15:36
  • Apparently you can also use globs with the `` approach – ssmith Nov 14 '19 at 15:52