10

I have a .gitignore file in the wwwroot folder of my project that I am trying to exclude from being published. The following code does not seem to work:

<ItemGroup>
  <Content Include="wwwroot\.gitignore">
    <CopyToPublishDirectory>Never</CopyToPublishDirectory>
  </Content>
</ItemGroup>

When I publish the project using the dotnet publish command, the .gitignore file is still found in the output directory.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

3 Answers3

25

You have to use Update like so:

<Content Update="wwwroot\.gitignore">
  <CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • 2
    Is it just me or that didn't work, the files are still published – Ayyash May 31 '17 at 14:52
  • Oh wait, got mixed up with CopyToOutoutDirectory – Ayyash May 31 '17 at 14:54
  • Would you care to elaborate on what's the difference between the `Include` and `Update` attribute? – lalibi Nov 08 '17 at 16:10
  • No idea but one works and the other does not. MSBuild is not well documented. – Muhammad Rehan Saeed Nov 08 '17 at 16:14
  • 3
    I think I found the answer. Everything inside the `wwwroot` folder is already included (which means that `CopyToPublishDirectory` is already defined). You have to, either `Remove` something and then re-`Include` it, or `Update` its metadata with some other option. – lalibi Nov 08 '17 at 17:37
1

Replace your code

<Content Include="wwwroot\.gitignore"> with 
<None Include="wwwroot\.gitignore">

How did I get to know this? While going through the code of .csproj file I came across this tag (None) that was put by visual studio in front of all publish profiles' file (.pubxml). so I tried with my files as well and it worked like a charm.

The MSDN article on the build action property explains the differences.

Saqib
  • 11
  • 3
0

Unfortunately the voted answer did not work for me. I had an ASP.NET Core web project and I was using Web Deploy to a remote server. I was trying to exclude a whole folder under wwwroot from being included in the deployment and after various trials and different combinations of things the only thing that worked for me was a combination of both:

  1. Exclude the folder from the project (i.e. right-click > Exclude from project)

AND

  1. Adding the following to my .csproj exactly as it is but changing wwwroot\\profiles to be the directory you want to exclude. You also have to repeat the whole snippet for each folder you want to exclude:
  <ItemGroup>
    <MsDeploySkipRules Include="CustomSkipFolder">
      <ObjectName>dirPath</ObjectName>
      <AbsolutePath>wwwroot\\profiles</AbsolutePath>
    </MsDeploySkipRules>
  </ItemGroup>
frezq
  • 653
  • 8
  • 18