'MSBuild not publishing Web.config to the root of the publish folder

I'm running the following MSBuild command from PowerShell to publish a solution file:

$Configuration = "Release"
$slnFullPath = "...\Solution.sln";
$PublishPath = "$RepoRoot\output-path\$Configuration"
$LogVerbosity = "q"
$msbuildOptions = @(
    $slnFullPath
    , "/t:Build"
    , "/p:Configuration=$Configuration"
    , "/p:DeployOnBuild=true"
    , "/p:publishUrl=$PublishPath" 
    , "-detailedSummary"
    , "-m"
    , "/p:BuildInParallel=true"
    , "-nologo"
    , "-binaryLogger:LogFile=$logsPath\$Configuration.binlog"
    , "-verbosity:$LogVerbosity"
    , "/consoleLoggerParameters:ShowTimestamp; ErrorsOnly; ShowEventId; PerformanceSummary; Summary;"
);

Write-Host "Command - & msbuild $msbuildOptions" -ForegroundColor DarkCyan
& msbuild $msbuildOptions

When I check the output folder, the web.config is not present in the root, but I can see it under the "bin" folder. The Web.config is currently marked as Content in the csproj file.

<Content Include="Web.config">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

I've noticed that if I rename the file (E.g. to Web2.config), then the file appears. Is this normal MSBuild behaviour? How can I force the Web.config to be published to the root of the output path?



Solution 1:[1]

Try changing the $msbuildOptions to @msbuildOptions. This is called splatting.

"Splatting is a method of passing a collection of parameter values to a command as a unit."

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.1

& msbuild @msbuildOptions

Solution 2:[2]

Sometimes you need a version of the .config for every configuration of your project. This is dependent on your configuration, if deploy the Release configuration this this would be Web.Release.config. But i think this version is not a real configuration but rather a xslt file.

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-6.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Otter
Solution 2 geraphl