Mohammad Hossein Amri
Mohammad Hossein Amri

Reputation: 2025

replace web.config in asp.net core vs 2017 on publish

i'm trying to replace the web.config with the production version.

I have the production version in ~\production\web.config (tilda as the root of my project folder).

I found this in the migration document

<ItemGroup>

  <Content Include="Views\**\*" PackagePath="%(Identity)" />

  <None Include="notes.txt" CopyToOutputDirectory="Always" />
  <!-- CopyToOutputDirectory = { Always, PreserveNewest, Never } -->

  <None Include="publishnotes.txt" CopyToPublishDirectory="Always" />
  <!-- CopyToPublishDirectory = { Always, PreserveNewest, Never } -->
</ItemGroup>

I tried to use something like this

<ItemGroup>   
     <None Include="_production\web.config" CopyToPublishDirectory="Always" /> 
</ItemGroup>

but in this way the folder and the file both will copy to the publish directory. it's a combination of mapping and publish and unfortunately the documentation is suffering from lack of example, so i tried to guess/figure it out by combining the mapping example with the publish.

first i tried this:

<None Include="_production\web.config" CopyToPublishDirectory="Always"  PackagePath="in/web.test"/>

so i was expecting to see a in folder in the publish directory but didn't.

i tried

<None Include="_production\web.config" CopyToPublishDirectory="Always"  PublishPath="in/web.test"/>

but didn't worked as well.

you can find the migration document here

Upvotes: 4

Views: 1017

Answers (1)

Set
Set

Reputation: 49789

Not sure if this is the most right way, but you may achieve this using Copy task directly:

<Target Name="CustomScript" AfterTargets="GeneratePublishRuntimeConfigurationFile">
    <Copy SourceFiles="_production\web.config" 
          DestinationFolder="$(PublishDir)" 
          OverwriteReadOnlyFiles="true"
          SkipUnchangedFiles="false" />
</Target>
  • AfterTargets="GeneratePublishRuntimeConfigurationFile" as we want to do replacement only after default web.config will be generated in publish folder
  • OverwriteReadOnlyFiles="true" - without this web.config cannnot be replaced

Upvotes: 3

Related Questions