Correctly enable XML documentation in ASP.Net Core for publish/build output

In May, 2017 I wrote this article which describes how to include the XML documentation in your publish output.

Outdated

My latest approach (for the sake of completeness) was (add to your csproj file):

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <ItemGroup>
      <DocFile Include="bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\$(TargetName).xml" />
    </ItemGroup>
    <Copy SourceFiles="@(DocFile)" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
  </Target>
  
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
  <DocumentationFile>bin\Debug\netcoreapp2.0\YOURPROJECT.xml</DocumentationFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
 <DocumentationFile>bin\Release\netcoreapp2.0\YOURPROJECT.xml</DocumentationFile>
  </PropertyGroup>

New and easier approach

But now you can just use

<PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

in your project file and visual studio takes care of debug/release folders and publishes your xml.

Solution found in this github issue.

Edit: This solution is still valid for .netcore3.1