How to replace a web.config setting with the current date while using webdeploy?

Viewed 1753

I'm using a web.config transform to replace settings with the settings of the selected solution configuration. However, I would like to add a setting that stores the datetime of the publish process. The reason for this is to be able to show a "Last published on" for my customers.

Using config transforms, is there a way to replace a setting with the current date?

3 Answers

I was able to do this in Visual Studio 2022 with a .NET 6 web application by using PowerShell scripts to add an <environmentVariable> with a Publish Timestamp to my web.config before publishing, then removing it after publishing. That way I don't get my local web.config altered in source control every time I publish.

I publish to a local folder, then deploy to a shared Production server with Beyond Compare. Having the deployed web.config include the timestamp will automatically restart my production IIS worker process, unlocking my main DLL, which fails to copy unless I touch my web.config.

  1. Create a Powershell script called beforePublish.ps1 with this code:

    $webConfig = "web.config"
    $webConfigXml = (gc $webConfig) -as [Xml]
    
    $publishTime = $webConfigXml.CreateElement("environmentVariable")
    $publishTime.SetAttribute("name", "PublishTime")
    $publishTime.SetAttribute("value", (Get-Date))
    $webConfigXml.configuration.location.'system.webServer'.aspNetCore.environmentVariables.AppendChild($publishTime)
    
    $webConfigXml.Save($webConfig)
    
    • This assumes you have an existing environmentVariable like this in the <aspNetCore> tag:
            <environmentVariables>
              <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
            </environmentVariables>
    
  2. Create a PowerShell script called afterPublish.ps1 with this code:

    $webConfig = "web.config"
    $webConfigXml = (gc $webConfig) -as [Xml]
    
    $nodes = $webConfigXml.SelectNodes("//environmentVariable[@name='PublishTime']")
    foreach($node in $nodes) { $node.ParentNode.RemoveChild($node) }
    
    $aspNetCore = $webConfigXml.SelectSingleNode("//aspNetCore")
    $aspNetCore.SetAttribute("processPath", $aspNetCore.GetAttribute("processPath").replace("Release", "Debug"))
    
    $webConfigXml.Save($webConfig)
    
    • As you can see I additionally reverted my <aspNetCore processPath='bin\Release\net6.0...'> back to Debug mode to keep my localhost unchanged by the publish to Release mode.
  3. Add this XML to the \Properties\PublishProfiles\FolderProfile.pubxml file inside the <Project> tag:

    <Target Name="AddTimestampToWebConfig" BeforeTargets="Publish">
        <Exec Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file D:\Documents\wwwroot\PortalApp\Properties\beforePublish.ps1" />
    </Target>
    <Target Name="RemoveTimestampFromWebConfig" AfterTargets="AfterPublish">
        <Exec Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file D:\Documents\wwwroot\PortalApp\Properties\afterPublish.ps1" />
    </Target>
    
Related