ASP.NET Core: Exclude or include files on publish

94,140

Solution 1

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.

Solution 2

In .csproj for Visual Studio versions 15.3 and higher, this keeps the files visible in Visual Studio (whereas "Content Remove" does not), and prevents the files from being published.

<ItemGroup>
    <Content Update="appsettings*.json" CopyToPublishDirectory="Never" />
</ItemGroup>

Solution 3

After Visual Studio 2017 15.3

Edit the .csproj file to manually exclude files/folder from being published

<ItemGroup>
  <Content Remove="src\**" />
  <Content Remove="node_modules\**" />
</ItemGroup>

ref: https://www.danielcrabtree.com/blog/273/fixing-the-duplicate-content-error-after-upgrading-visual-studio-2017

Solution 4

With Visual Studio 2017 (tested in 15.6.5), you can right-click on the file in the Solution Explorer and set the Build Action to None.

It will update your .csproj file like this:

<ItemGroup>
  <Content Remove="appsettings.Development.json" />
  <Content Remove="appsettings.json" />
  <Content Remove="npm-shrinkwrap.json" />
  <Content Remove="package.json" />
  <Content Remove="tsconfig.json" />
</ItemGroup>

<ItemGroup>
  <None Include="appsettings.Development.json" />
  <None Include="appsettings.json" />
  <None Include="npm-shrinkwrap.json" />
  <None Include="package.json" />
  <None Include="tsconfig.json" />
</ItemGroup>

Hope this helps.

Solution 5

For Visual Studio 2019, I managed to exclued a wwwroot subfolder named "dummy" (including all subfolders of "dummy") from publish output using the following code:

<ItemGroup>
  <Content Update="wwwroot\dummy\**\*">
    <CopyToPublishDirectory>Never</CopyToPublishDirectory>
  </Content>
</ItemGroup>

Note: The requirement was to keep the wwwroot and its subfolder(s) included in project but just exclude while publishing.

Share:
94,140
hcp
Author by

hcp

Updated on July 05, 2022

Comments

  • hcp
    hcp almost 2 years

    There were before aspdotnet1.0 include/exclude sections on project.json file

    {
      "exclude": [
        "node_modules",
        "bower_components"
      ],
      "publishExclude": [
        "**.xproj",
        "**.user",
        "**.vspscc"
      ]
    }
    

    Where is this section in ASP.NET Core 1.1 (there is no project.json)? Are there similar sections on .csproj file or .pubxml?

  • hcp
    hcp about 7 years
    Yes, I know about csproj instead of project.json, but where is include/exclude secion?
  • Shridhar R Kulkarni
    Shridhar R Kulkarni about 7 years
    Updated my answer.
  • Tagc
    Tagc almost 7 years
    MigratePublishOptionsRule link is dead.
  • Bluesight
    Bluesight about 6 years
    This is the solution that works now. CopyToPublishDirectory or CopyToOutputDirectory haven't worked at all after 15.3
  • tala9999
    tala9999 almost 6 years
    This really helps me.
  • HH321
    HH321 almost 6 years
    This is a much better solution than accepted answer! Note the Update attribute so that you can apply it to files which have been already included in project by default because they are under the root folder
  • Ben Anderson
    Ben Anderson almost 6 years
    This answer is for project before Visual Studio 2017 15.3. Please refer to @Wagner-Pereira for the latest projects
  • ArieKanarie
    ArieKanarie about 5 years
    I also agree. This also works in VS 2019, version 16.0.0 (just tested)
  • ahong
    ahong almost 5 years
  • scubaFun
    scubaFun almost 4 years
    Confirmed that this still works. Tried everything above and nothing worked, but this one did.
  • Chris Ward
    Chris Ward over 3 years
    I have <ItemGroup> <None Update="readme.html"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirect‌​ory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDire‌​ctory> </None> <None Update="readme.md"> <CopyToOutputDirectory>Never</CopyToOutputDirectory> </None> </ItemGroup> I want the readme.html to be copied during publish but it's not doing it.
  • ggo
    ggo about 3 years
    Just seen your answer after finding the same solution for vs2019^^. So I'm only adding this information: this solution also works for vs2019 (and is the best and easiest way to prevent a file from being published IMHO).
  • Itamaram
    Itamaram almost 3 years
    Why add that second condition for release? Isn't Never the default behaviour anyway?
  • Gerardo Verrone
    Gerardo Verrone about 2 years
    I can comfirm this solution has worked for me on Visual Studio 2022 / .Net Core 6
  • Akmal Salikhov
    Akmal Salikhov about 2 years
    Can I specify here that only appsettings file from current project must be published? I need to exclude appsettings files from dependence projects.