ASP.NET Core: Exclude or include files on publish
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>
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.
hcp
Updated on July 05, 2022Comments
-
hcp almost 2 years
There were before
aspdotnet1.0
include/exclude sections onproject.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 about 7 yearsYes, I know about csproj instead of project.json, but where is include/exclude secion?
-
Shridhar R Kulkarni about 7 yearsUpdated my answer.
-
Tagc almost 7 yearsMigratePublishOptionsRule link is dead.
-
Bluesight about 6 yearsThis is the solution that works now. CopyToPublishDirectory or CopyToOutputDirectory haven't worked at all after 15.3
-
tala9999 almost 6 yearsThis really helps me.
-
HH321 almost 6 yearsThis 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 almost 6 yearsThis answer is for project before Visual Studio 2017 15.3. Please refer to @Wagner-Pereira for the latest projects
-
ArieKanarie about 5 yearsI also agree. This also works in VS 2019, version 16.0.0 (just tested)
-
ahong almost 5 yearsWorks for me. Reference here: docs.microsoft.com/en-us/aspnet/core/host-and-deploy/…
-
scubaFun almost 4 yearsConfirmed that this still works. Tried everything above and nothing worked, but this one did.
-
Chris Ward over 3 yearsI 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.
-
ggo about 3 yearsJust 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 almost 3 yearsWhy add that second condition for release? Isn't
Never
the default behaviour anyway? -
Gerardo Verrone about 2 yearsI can comfirm this solution has worked for me on Visual Studio 2022 / .Net Core 6
-
Akmal Salikhov about 2 yearsCan I specify here that only appsettings file from current project must be published? I need to exclude appsettings files from dependence projects.