automaton
automaton

Reputation: 3058

.NET Core csproj PublishProfileName has a value when building

I'm using the following sample to dynamically produce the Web.config file:

https://sebnilsson.com/blog/asp-net-transform-web-config-with-debug-release-on-build/

The change includes this in the .csproj which works in regular .NET:

<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">

However, when a similar line is used in .NET Core, it doesn't work because PublishProfileName is actually 'FileSystem'. What I'd like is a way to detect publish (vs build) to avoid the issue of modifying Web.config twice. Here's the modified part for .NET Core:

  <Target Name="TestTarget" AfterTargets="Build">
    <Message Importance="High" Text="Firing on publish due to PublishProfile being '$(PublishProfileName)'" />
    <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
  </Target>

Does anyone know how to do the same thing in .NET core as was done in .NET to stop double modification of web.config when dynamically produced?

Upvotes: 0

Views: 572

Answers (1)

LoLance
LoLance

Reputation: 28086

What I'd like is a way to detect publish (vs build) to avoid the issue of modifying Web.config twice.

The easiest way i think is using Condition="'$(DeployOnBuild)' != 'true'" in your target. Then this target will only be executed during build process but not publish process.

<Target Name="TestTarget" Condition="'$(DeployOnBuild)' != 'true'" AfterTargets="Build">
    <Message Importance="High" Text="Firing on publish due to PublishProfile being '$(PublishProfileName)' and " />
    <!--<TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />-->
  </Target>

This will make sure when you publishing the project in VS by FileSystem, it will only execute the target once in build instead of twice in build and publish. I'm not sure why you set the AfterTargets="Build", according to the article you mentioned above, it might be BeforeTargets="Build".

Upvotes: 1

Related Questions