My project contains both appsettings.json and a default appsettings.Production.json files. However an admin may edit the Production file manually on the server and it would get overwritten if I publish it again.

Can I somehow specify only this file to not be overwritten if such file already exists?

Note that this question which uses CopyToPublishDirectory="PreserveNewest" does not work because apparently it doesn't work for Web Deploy (IIS Publish) and I want it to not overwrite even if the file in the project is newer (i.e. the admin edits it before we modify it in the code).

1

There are 1 best solutions below

2
YurongDai On

You can exclude specific files or folders when you build a web package, by creating a custom .wpp.targets file in the same folder as your web application project file.

  1. Create a custom project file named [project name].wpp.targets in the same folder as your project file.

  2. In the .wpp.targets file, add an ItemGroup element.

  3. In the ItemGroup element, add ExcludeFromPackageFolders and ExcludeFromPackageFiles items to exclude specific files and folders as required.

The following is the basic structure of this .wpp.targets file:

<Project ToolsVersion="4.0" 
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>   
    <ExcludeFromPackageFolders Include="[semi-colon-separated folder list]">
      <FromTarget>[arbitrary metadata value]</FromTarget>
    </ExcludeFromPackageFolders>
    <ExcludeFromPackageFiles Include="[semi-colon-separated file list]">
      <FromTarget>[arbitrary metadata value]</FromTarget>
    </ExcludeFromPackageFiles>
  </ItemGroup>
</Project>

Then you need to add the .wpp.targets file to the web application project and use this file to exclude appsettings.json and appsettings.Production.json from the web package when building the project.

For more details, please refer to this document: Excluding Files and Folders from Deployment