Remove unnecessary appssettings.json when publishing a program

Page updated :

Environment

Visual Studio
  • Visual Studio 2019
ASP.NET Core
  • 3.1

About published appssettings.json

ASP.NET Core-created programs now load the settings "appssettings.json" and "appssettings.production.json" depending on the environment. You don't have to worry about publishing directly to the server and not having to edit appsettings.json. In fact, if you look at the list of published files, you will see that "appssettings. You can also see that unnecessary files such as "Development.json" have also been published.

It's not a problem if you don't edit appssettings.json after you publish the program directly to the server as described above. If you want to publish as a file once without publishing directly to the server, or if you want to edit appssettings.json directly on the server later, The presence of multiple appsettings.json can trigger a configuration error, etc., which is very dangerous.

You can manually delete the unwanted appssettings.json each time you publish, but this can also be a mistake because it goes through people's hands.

This section describes how to remove unwanted appssettings.json at the time of publication.

Configuration steps to automatically delete unwanted appssettings.json at the time of publication

First, publish once and create a profile. Change the name of the profile to make it easier to understand. (Here it is set with the profile name Release)

When published, a file called "PublishProfiles\<Profile Name>.pubxml" is created in the Properties folder, so double-click it to open it.

Since the contents are XML files, add the appsettings.json that you want to exclude to the location of the following comments. If there is more than one, add "Content" and "None" as many as necessary and set "Remove" and "Include" to the target file name.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <ProjectGuid>d988b0db-dad2-4308-baa1-6d1c89a074db</ProjectGuid>
    <SelfContained>false</SelfContained>
    <publishUrl>bin\Release\netcoreapp3.1\publish\</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
  </PropertyGroup>
  <!-- ここから追加 -->
  <ItemGroup>
    <Content Remove="appsettings.Development.json" />
    <None Include="appsettings.Development.json" />
  </ItemGroup>
  <!-- ここまで追加 -->
</Project>

Please try to publish once you have saved. You can verify that the specified file has not been published.