asp.net-coremsbuildcsproj

Copy folder on publish ASP.NET Core


I need to copy the Views folder to the publish folder (on publish), and I have this in my .csproj.

(Note: I don't need to change the views on runtime, different usage)

<Target Name="CopyViews" BeforeTargets="Build;Publish">
    <ItemGroup>
        <ViewsFiles Include="Views\**\*" />
    </ItemGroup>
    <Copy SourceFiles="@(ViewsFiles)" DestinationFolder="$(OutDir)\Views" />
</Target>

This works for build, the Views gets copied to the bin folder, but when I publish it doesn't get copied in the publish folder.


Solution

  • You could try configure them separately:

        <Target Name="CopyViews_build" BeforeTargets="Build">
            <ItemGroup>
                <ViewsFiles Include="Views\**\*" />
            </ItemGroup>
            <Copy SourceFiles="@(ViewsFiles)" DestinationFolder="$(OutDir)\Views" />
        </Target>
        <Target Name="CopyViews_publish" BeforeTargets="Publish">
            <ItemGroup>
                <ViewsFiles Include="Views\**\*" />
            </ItemGroup>
            <Copy SourceFiles="@(ViewsFiles)" DestinationFolder="$(PublishDir)\Views" />
        </Target>
    

    Publish Test
    enter image description here