ASP.NET Core: Exclude or Include Files on Publish

ASP.NET Core: Exclude or include files on publish


From documentation: if you wish to specify, for example, some files to get published with your app, you can still use the known mechanisms in csproj for that (for example, the <Content> element).

There is a CopyToPublishDirectory attribute for ItemGroup elements that determines whether to copy the file to the publish directory and can have one of the following value:

  • Always,
  • PreserveNewest
  • Never

Note, that there is also similar CopyToOutputDirectory attribute for output folder.

Example (from here):

<ItemGroup>

<None Include="notes.txt" CopyToOutputDirectory="Always" />
<!-- CopyToOutputDirectory = { Always, PreserveNewest, Never } -->

<Content Include="files\**\*" CopyToPublishDirectory="PreserveNewest" />
<None Include="publishnotes.txt" CopyToPublishDirectory="Always" />
<!-- CopyToPublishDirectory = { Always, PreserveNewest, Never } -->
</ItemGroup>

If you are interesting how project.json -.csproj migration use CopyToPublishDirectory attribute to migrate publish options, you may look into MigratePublishOptionsRule class in dotnet cli repo.

ASP.NET core include files on publishing

I find solution:

<ItemGroup>
<ReportFiles Include="..\Reports\**\*.rdlc">
<Path>\Reports</Path>
</ReportFiles>
</ItemGroup>

<Target Name="PrepublishScript"
BeforeTargets="PrepareForPublish">
<Copy SourceFiles="@(ReportFiles)"
DestinationFolder="$(PublishDir)\%(Path)"
SkipUnchangedFiles="false" />
</Target>

How to exclude files from being published in ASP.NET Core?

By default all code files in a directory containing a project.json are included in the project. You can control this with the include/exclude sections of the project.json.

The most common sections that you will see for including and excluding files are:

{
"compile": "*.cs",
"exclude": [
"node_modules",
"bower_components"
],
"publishExclude": [
"**.xproj",
"**.user",
"**.vspscc"
]
}
  • The compile section specifies that only .cs files will be compiled.
  • The exclude section excludes any files in the node_modules and
    bower_components directories. Even if sections have .cs extensions.
  • The publishExclude section allows you to exclude files from the
    publish output of your project. In this example, all .xproj, .user,
    and .vspscc files from the output of the publish command.

From here

.NET Core include folder in publish

Adding this:

<ItemGroup> 
<Content Include="AppData\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

to your .csproj file will copy AppData folder if it's not empty. For empty AppData folder you can use this workaround:

<Target Name="CreateAppDataFolder" AfterTargets="AfterPublish">
<MakeDir Directories="$(PublishDir)AppData" Condition="!Exists('$(PublishDir)AppData')" />
</Target>

This will create AppData folder after publish if it won't be already included in output. Meaning this will create AppData folder only if it's empty while publishing.

How can I exclude files from Visual Studio, but still deploy them upon publishing?

Here is what we have done to (hopefully) resolve this issue for us:

  1. Added the following line to our WebApp.csproj file within the PropertyGroup:

    <ProjectGroup>
    <DefaultItemExcludes>$(DefaultItemExcludes);wwwroot\hugecontentfolder\**</DefaultItemExcludes>
    </ProjectGroup>
  2. The huge content folder stays where it is in source control, this simplifies things as we need those files there to be served locally for development

  3. We updated our Azure DevOps pipelines with new tasks to copy the contents of that folder from source control into the build artifact staging directory

  4. Updated the Dotnet Publish Azure Devops task to no longer zip it's output

  5. Another new Azure DevOps task to Archive the build artifact staging directory (which now has the non-zipped dotnet publish output, as well as the huge content folder output in the correct location) into a zip file for publishing.

Can't exclude web.config file while publishing using dotnet publish command

According to this github issue, you should add the following section in the .csproj

<PropertyGroup>
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup>


Related Topics



Leave a reply



Submit