63

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?

hypercodeplace
  • 2,506
  • 6
  • 21
  • 31

10 Answers10

107

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.

AaronLS
  • 34,709
  • 17
  • 135
  • 191
Set
  • 40,147
  • 18
  • 114
  • 133
47

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>
Craig Wilson
  • 491
  • 4
  • 6
  • 3
    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 – HH321 Jul 06 '18 at 15:44
  • 2
    I also agree. This also works in VS 2019, version 16.0.0 (just tested) – ArieKanarie May 02 '19 at 08:19
  • 2
    Works for me. Reference here: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.2#exclude-files – ahong May 22 '19 at 13:00
  • Confirmed that this still works. Tried everything above and nothing worked, but this one did. – scubaFun Jul 01 '20 at 12:05
33

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

Wagner Pereira
  • 834
  • 10
  • 11
  • 4
    This is the solution that works now. CopyToPublishDirectory or CopyToOutputDirectory haven't worked at all after 15.3 – Bluesight Mar 21 '18 at 15:50
17

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.

György Balássy
  • 2,679
  • 1
  • 25
  • 21
  • 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). – ggo May 07 '21 at 11:21
10

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.

Mohsin Mehmood
  • 3,707
  • 2
  • 10
  • 17
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.

Brady Moritz
  • 7,852
  • 8
  • 58
  • 96
3

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>

**

jlo-gmail
  • 1,974
  • 23
  • 39
3

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

Randy Burden
  • 2,331
  • 20
  • 31
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/.

Community
  • 1
  • 1
Shridhar R Kulkarni
  • 4,794
  • 2
  • 27
  • 49
0

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
Anish Kutti
  • 109
  • 4