Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop copying referenced project's AppSettings.json and web.config to output folder

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>
like image 393
Liero Avatar asked Oct 11 '25 15:10

Liero


1 Answers

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

like image 137
user1589652 Avatar answered Oct 14 '25 05:10

user1589652