ASP.NET Core: Exclude or include files on publish
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.
ASP.NET core include files on publishing
I find solution:
<ItemGroup>
<ReportFiles Include="..\Reports\**\*.rdlc">
<Path>\Reports</Path>
</ReportFiles>
</ItemGroup>
<Target Name="PrepublishScript"
BeforeTargets="PrepareForPublish">
<Copy SourceFiles="@(ReportFiles)"
DestinationFolder="$(PublishDir)\%(Path)"
SkipUnchangedFiles="false" />
</Target>
How to exclude files from being published in ASP.NET Core?
By default all code files in a directory containing a project.json are included in the project. You can control this with the include/exclude sections of the project.json.
The most common sections that you will see for including and excluding files are:
{
"compile": "*.cs",
"exclude": [
"node_modules",
"bower_components"
],
"publishExclude": [
"**.xproj",
"**.user",
"**.vspscc"
]
}
- The compile section specifies that only .cs files will be compiled.
- The exclude section excludes any files in the node_modules and
bower_components directories. Even if sections have .cs extensions. - The publishExclude section allows you to exclude files from the
publish output of your project. In this example, all .xproj, .user,
and .vspscc files from the output of the publish command.
From here
.NET Core include folder in publish
Adding this:
<ItemGroup>
<Content Include="AppData\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
to your .csproj
file will copy AppData
folder if it's not empty. For empty AppData
folder you can use this workaround:
<Target Name="CreateAppDataFolder" AfterTargets="AfterPublish">
<MakeDir Directories="$(PublishDir)AppData" Condition="!Exists('$(PublishDir)AppData')" />
</Target>
This will create AppData
folder after publish if it won't be already included in output. Meaning this will create AppData
folder only if it's empty while publishing.
How can I exclude files from Visual Studio, but still deploy them upon publishing?
Here is what we have done to (hopefully) resolve this issue for us:
Added the following line to our WebApp.csproj file within the PropertyGroup:
<ProjectGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);wwwroot\hugecontentfolder\**</DefaultItemExcludes>
</ProjectGroup>The huge content folder stays where it is in source control, this simplifies things as we need those files there to be served locally for development
We updated our Azure DevOps pipelines with new tasks to copy the contents of that folder from source control into the build artifact staging directory
Updated the Dotnet Publish Azure Devops task to no longer zip it's output
Another new Azure DevOps task to Archive the build artifact staging directory (which now has the non-zipped dotnet publish output, as well as the huge content folder output in the correct location) into a zip file for publishing.
Can't exclude web.config file while publishing using dotnet publish command
According to this github issue, you should add the following section in the .csproj
<PropertyGroup>
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup>
Related Topics
This.Visible Is Not Working in Windows Forms
The Type of One of the Expressions in the Join Clause Is Incorrect in Entity Framework
ASP.NET Core: Exclude or Include Files on Publish
How to Call a Non-Static Method from a Static Method in C#
@Html.Dropdownlistfor How to Set Default Value
Is It Worthwhile to Initialize the Collection Size of a List<T> If It's Size Reasonably Known
Combination of List<List<Int>>
Get All Controls of a Specific Type
How to Fix a .Net Windows Application Crashing at Startup with Exception Code: 0Xe0434352
How to Deserialize a JSON Property That Can Be Two Different Data Types Using JSON.Net
Deserialize JSON Array Stream One Item at a Time
Redirecting Standard Input of Console Application
How to Create a Hashcode in .Net (C#) for a String That Is Safe to Store in a Database
How to Use a Reserved Keyword as an Identifier in My JSON Model Class
Unity How to Make a Visual Joystick in Unity
How to Serialize/Deserialize a Custom Collection with Additional Properties Using JSON.Net