I have ASP.NET Core 5 project
<Project Sdk="Microsoft.NET.Sdk.Web">
that contains appsettings.json. Build action is not customized: Content, Copy if Newer.
How do I reference this project in another csproj so that the appsettings.json won't be included in output folder?
<ProjectReference Include="..\..\src\app\App.csproj">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>compile</IncludeAssets>
<ExcludeAssets>contentfiles; build; analyzers; buildtransitive</ExcludeAssets>
</ProjectReference>
I was able to solve it with these two targets in primary project (one that you are publishing)
<Target Name="DeleteAllAppsettings" AfterTargets="PostBuildEvent">
<ItemGroup>
<AppSettingsFilesToDelete Include="$(OutputPath)appsettings*.json" />
</ItemGroup>
<Delete Files="@(AppSettingsFilesToDelete)" />
<Message Text="Deleted appsettings JSON files: @(AppSettingsFilesToDelete)" Importance="high" />
</Target>
<Target Name="CopyTestProjectAppSettings" AfterTargets="PostBuildEvent">
<ItemGroup>
<AppSettingsJsonFiles Include="appsettings*.json" />
</ItemGroup>
<Copy SourceFiles="@(AppSettingsJsonFiles)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="false" />
<Message Text="Copied test project appsettings JSON files to output directory." Importance="high" />
</Target>
You can copy it and also extend for *.config files if you want. Keep in mind that it will copy all json files from project root directory that starts with name appsettings even if it marked as Content with Build action to not copy or so. If you want more granular behaviour you may need to specify files directly rather than work with all of them as in provided example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With