Set web.config transform in Asp.NET Core

There is a well-documented tool on github for xdt-transformations. Also it doesn't depend on command, both of dotnet publish and dotnet msbuild works fine


With the latest version of dotnet cli (2.1.400 or greater), you can just set this msbuild property $(EnvironmentName) and publish tooling will take care of adding ASPNETCORE_ENVIRONMENT environmentVariable to the web.config with the specified environment name.

Also, XDT support is available starting 2.2.100-preview1.

Sample: https://github.com/vijayrkn/webconfigtransform/blob/master/README.md


IIS Web Deploy ASP.NET Core (2.1) in Visual Studio 2017 (VS2017)

First do this: (ref:https://github.com/nil4/dotnet-transform-xdt#-use-with-msbuildcsproj-tooling)

  1. Install package - dotnet add package DotNet.Xdt --version 2.1.0
  2. Modify .csproj - add package - refer github
  3. Modify .csproj - add transform code (ApplyXdtConfigTransform) at the end - refer github
  4. Add web.DEV_Server.config transfor file by right-clicking on DEV_Server.pubxml
  5. Added following to web.DEV_Server.config

<environmentVariable xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Development" xdt:Transform="SetAttributes" />

  1. Modify DEV_Server.pubxml to modify following setting value.

<LastUsedBuildConfiguration>DEV_Server</LastUsedBuildConfiguration>

  1. Validate Connection & Publish

Deploy still uploads other config files, not sure how to stop that.


This worked for me:

  1. Add web.release.config file to the project root.
  2. In Visual Studio 2017, Publish using Web Deploy (make sure it is set to Release). Settings will automatically be picked up.

Sample transformation:

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <system.webServer>
          <aspNetCore>
            <environmentVariables>
              <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="PRODUCTION" xdt:Locator="Match(name)" xdt:Transform="SetAttributes" />
            </environmentVariables>
          </aspNetCore>
        </system.webServer>
    </configuration>

Update: If you want to remove web.config.release file and others on publish, simply edit your .csproj file and add something like this:

  <ItemGroup>
    <Content Remove="appsettings.Development.json" />
    <Content Remove="web.release.config" />
  </ItemGroup>
  <ItemGroup>
    <None Include="appsettings.Development.json" />
    <None Include="web.release.config" />
  </ItemGroup>