ASP.NET Core: Exclude or include files on publish
Asked Answered
I

13

85

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?

Improvised answered 10/3, 2017 at 6:50 Comment(0)
C
145

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.

Cerebral answered 10/3, 2017 at 8:36 Comment(4)
MigratePublishOptionsRule link is dead.Flush
This answer is for project before Visual Studio 2017 15.3. Please refer to @Wagner-Pereira for the latest projectsDialogism
I have <ItemGroup> <None Update="readme.html"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </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.Stover
This work using .Net 6 CLIScorn
G
85

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>

For excluding the content of a folder you need to write:

<!-- Excluding the ClientApp folder. -->
<Content Update="ClientApp\**" CopyToPublishDirectory="Never" />
Grisham answered 8/5, 2018 at 17:44 Comment(7)
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 folderCapful
I also agree. This also works in VS 2019, version 16.0.0 (just tested)Remise
Works for me. Reference here: learn.microsoft.com/en-us/aspnet/core/host-and-deploy/…Escobedo
Confirmed that this still works. Tried everything above and nothing worked, but this one did.Polyandry
I can comfirm this solution has worked for me on Visual Studio 2022 / .Net Core 6Jaquelynjaquenetta
Can I specify here that only appsettings file from current project must be published? I need to exclude appsettings files from dependence projects.Proofread
It is worth it to mention that for excluding the content of a folder you need to write <!-- Excluding the ClientApp folder. --> <Content Update="ClientApp\**" CopyToPublishDirectory="Never" />Rejoice
B
41

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

Boyne answered 7/2, 2018 at 18:54 Comment(6)
This is the solution that works now. CopyToPublishDirectory or CopyToOutputDirectory haven't worked at all after 15.3Spohr
Unfortunately this removes your modules also from the VS solution explorer and you'll lose tooling support. CopyToPublishDirectory works fine for me with 17.5.Ewart
This is the only way we could get this to work also in VS 2022. Not sure if there are other publish settings that make the old way work. We ended up updating all the files using the properties panel to None->DoNotCopy and the find-replace the csproj file with from <None Remove to <Content RemovePleomorphism
This is the only solution working for Rider.Longwood
On Rider, using Remove attribute also hides the file(s) in the solution explorer. Using Update in place of Remove with CopyToPublishDirectory="Never" fixed the issue while preserving the file's visibility in the solution explorer.Longwood
This method doesn't exclude files included by referencing a NuGet package, which contains the unwanted file.Mainspring
A
23

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.

Abducent answered 19/4, 2018 at 4:10 Comment(1)
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).Vltava
W
18

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.

Worldly answered 25/5, 2020 at 16:59 Comment(0)
J
12

To exclude files on publish, edit your publish file, e.g. FolderProfile.pubxml, and add the appropriate XML elements shown below to include/exclude files and folders.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" 
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <PublishFramework />
    <ProjectGuid>afa9f185-7ce0-4935-9da1-ab676229d68a</ProjectGuid>
    <publishUrl>bin\Release\PublishOutput</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
  </PropertyGroup>
  <ItemGroup>

    <!-- CopyToPublishDirectory = { Always, PreserveNewest, Never } -->

    <!-- Copying a file located outside of the project into the published site's wwwroot folder. -->
    <ResolvedFileToPublish Include="..\ReadMe2.md">
      <RelativePath>wwwroot\ReadMe2.md</RelativePath>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </ResolvedFileToPublish>

    <!-- Excluding the wwwroot\Content folder. -->
    <Content Update="wwwroot\Content\**\*" CopyToPublishDirectory="Never" />

    <!-- Excluding the Views\Home\About2.cshtml file. -->
    <Content Update="Views\Home\About2.cshtml" CopyToPublishDirectory="Never" />
  </ItemGroup>
</Project>

See also: Visual Studio publish profiles - Selective file inclusion

Judenberg answered 12/3, 2021 at 20:52 Comment(1)
This method doesn't exclude files included by referencing a NuGet package, which contains the unwanted file.Mainspring
J
8

The Solution from @Craig Wilson worked great to pubish with Visual Studio 2022 and Core 6

The following is a sample to exclude all appsettings except for appsettings.json and also the appsetting.QA.json when you setup an Environment Variable at a system level like ASPNETCORE_ENVIRONMENT=QA

In windows, you can also setup the environment variable on the fly in the same powershell you use to run the dotnet pubish command.

$env:ASPNETCORE_ENVIRONMENT = 'QA'

That variable will be drop when you close that powershell

    <!-- Publish Settings -->
    <ItemGroup>
        <Content Update="appsettings.*.json">
        <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </Content>  
        <Content Update="appsettings.$(ASPNETCORE_ENVIRONMENT).json">
        <CopyToPublishDirectory>Always</CopyToPublishDirectory>
        </Content>  
        <Content Update="appsettings.$(ASPNETCORE_ENVIRONMENT).json">
        <CopyToPublishDirectory>Always</CopyToPublishDirectory>
        </Content>      
    </ItemGroup>   

Please note, in Visual studio 2022 all files are included by default and you just need to use Update

Jaquelynjaquenetta answered 9/3, 2022 at 5:21 Comment(1)
This method doesn't exclude files included by referencing a NuGet package, which contains the unwanted file.Mainspring
H
4

In my case I need local.settings.json locally during debug, but I do not want it included in my WebDeploy zip file during Release builds or Publish:

  <ItemGroup>
    <Content Include="..\local.settings.json" Link="local.settings.json" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="..\local.settings.json" Link="local.settings.json" Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

**

Headed answered 4/2, 2020 at 14:13 Comment(1)
Why add that second condition for release? Isn't Never the default behaviour anyway?Unemployable
C
3

I noticed that my folders with a few files in them were not being published- I tried right clicking the folders in the project to see if I could select an option to include the folder with the deployment- it's not there, but I did find if I select the files inside the folder and mark them to copy on deployment, it will copy the files and create their folder in the process.

This helps if your folder has files, but doesn't help if your folders are empty.

Chalet answered 16/4, 2018 at 4:34 Comment(1)
This really helps me.Billon
B
2

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

You can also refer this

For web deployment see https://blogs.msdn.microsoft.com/webdev/2010/04/22/web-deployment-excluding-files-and-folders-via-the-web-applications-project-file/.

project.json has been now replaced by csproj. You can read about it more on https://www.stevejgordon.co.uk/project-json-replaced-by-csproj.

For Upgrading Existing .NET Core 1.0 Projects or for using Using .NET Core 1.1 you can read https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-net-core-1-1/.

Bibbye answered 10/3, 2017 at 6:58 Comment(2)
Yes, I know about csproj instead of project.json, but where is include/exclude secion?Improvised
Updated my answer.Bibbye
G
2

In Visual Studio 2019,

  1. Right-click the file -> Properties
  2. Copy To Output Directory-> Copy always

enter image description here

  1. Save the solution and try publish
Gaming answered 6/11, 2020 at 20:10 Comment(0)
B
0

In visual studio 2022: Unload csproj, open the file To add content and the subfolders, insert:

<ItemGroup>
        <None Update="StaticContent\**">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    </ItemGroup>
Backstitch answered 19/1, 2022 at 18:51 Comment(0)
B
-2

Visual Studio 2019:

  1. Go to the "Properties" for the file in the "Solution Explorer"
  2. Change the "Copy to Output Directory" property to the desired value ("Copy always", "Do not copy", "Copy if newer").
Binion answered 26/10, 2021 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.