title: Azure Pipelines with .Net Core - Transform web.config for each environment
body: <p>We have been having an issue migrating to Azure Devops with .Net Core to deploy to our on-prem Windows servers. It's probably due to a lack of knowledge of the platform but nonetheless...</p>
  <p><strong>The problem</strong> we are trying to figure out is when following a &quot;build once and deploy everywhere&quot; structure, how do we transform our web.config file to set the &quot;ASPNETCORE_ENVIRONMENT&quot; env variable to the correct environment that is being deployed. Currently our apps need that variable set because we have our appsettings split between 2 files, appSettings.json and appSettings.Env.json, where Env is the current environment we are deploying to (Dev or Prod).</p>
  <p><strong>For example:</strong>
  Azure Pipelines builds our project and the resulting web.config in the output artifact has the ASPNETCORE_ENVIRONMENT variable set to &quot;Development&quot;. That's great since we then deploy that artifact to our dev environment. But what happens when we release that to our prod stage? That web.config still has the ASPNETCORE_ENVIRONMENT variable set to &quot;Development&quot;.</p>
  <p>What is the proper (2021) way to transform that config? should I not even use that variable in the web.config and change up how we interact with our appSettings file?</p>
  <p>I've read through many article and questions about similar issues but not many related to the ASPNETCORE_ENVIRONMENT env variable. Some say use the &quot;Replace token task&quot;. Some say use config transforms (they don't work for ASPNETCORE_ENVIRONMENT).</p>
  <p>Not sure where to go from here.</p>
  <p><strong>What I've recently tried:</strong></p>
  <p><a href="https://stackoverflow.com/questions/37260243/transform-web-config-for-azure-website-deployment-for-each-release-environment">Transform web.config for Azure Website Deployment for each release environment</a></p>
  <p><a href="https://stackoverflow.com/questions/61628029/azure-pipelines-config-transform-based-on-stage">Azure Pipelines Config Transform based on Stage</a></p>
  <p><strong>My generated web.config file on publish:</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;configuration&gt;
  &lt;location path=&quot;.&quot; inheritInChildApplications=&quot;false&quot;&gt;
  &lt;system.webServer&gt;
    &lt;handlers&gt;
      &lt;add name=&quot;aspNetCore&quot; path=&quot;*&quot; verb=&quot;*&quot; modules=&quot;AspNetCoreModuleV2&quot; resourceType=&quot;Unspecified&quot; /&gt;
    &lt;/handlers&gt;
    &lt;aspNetCore processPath=&quot;dotnet&quot; arguments=&quot;.\WebApi.dll&quot; stdoutLogEnabled=&quot;false&quot; stdoutLogFile=&quot;.\logs\stdout&quot; hostingModel=&quot;inprocess&quot;&gt;
      &lt;environmentVariables&gt;
        &lt;environmentVariable name=&quot;ASPNETCORE_ENVIRONMENT&quot; value=&quot;Development&quot; /&gt;
      &lt;/environmentVariables&gt;
    &lt;/aspNetCore&gt;
  &lt;/system.webServer&gt;
  </code></pre>
    
  
  <p><strong>Azure Pipeline file</strong></p>
  <pre><code>name: WebApp-$(Date:yyyyMMdd).$(rev:rr)
  
  trigger:
  branches:
   include:
   - master
  
  variables:
    major: 2
    minor: 0
    patch: 0
    solution: '**/*.sln'
    buildPlatform: 'Any CPU'
    buildConfiguration: 'Release'
    Parameters.WebsiteName: 'webapp'
  
  jobs:
  - job: Build  
    pool:
      vmImage: 'windows-latest'
  
  steps:
    - task: DotNetCoreCLI@2
    displayName: 'dotnet restore'
    inputs:
      command: 'restore'
      projects: '$(solution)'
  
  - task: VSBuild@1
    inputs:
      solution: '$(solution)'
      msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation=&quot;$(build.artifactStagingDirectory)\WebApp.zip&quot; /p:DeployIisAppPath=&quot;webapp&quot; /p:EnvironmentName=&quot;Development&quot;'
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'
  
  - task: VSTest@2
    inputs:
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'
  
  - task: ArchiveFiles@2
    displayName: 'Create Zip Archive'
    inputs:
      rootFolderOrFile: '$(build.artifactStagingDirectory)/'
      includeRootFolder: false
      archiveType: zip
      archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildNumber).zip
      replaceExistingArchive: true  
  
  - task: DotNetCoreCLI@2
    inputs:
      command: 'publish'
      publishWebProjects: true
  
  - task: PublishBuildArtifacts@1
    displayName: 'Web Artifact'
  </code></pre>