Search code examples
asp.net-coreweb.config-transform

Set web.config transform in Asp.NET Core


I've just came across with problem of web.config transformation in asp.net core.

There are two files: base web.config and web.prod-zone-a.config. My aim is to use transformation inside web.prod-zone-a.config when publishing my project. I have the following "prod-zone-a" configuration settings in .csproj:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'prod-zone-a|AnyCPU' ">
    <IntermediateOutputPath>obj\Debug\netcoreapp1.1</IntermediateOutputPath>
    <DebugSymbols>true</DebugSymbols>
    <Optimize>false</Optimize>
    <DefineConstants>TRACE;DEBUG;NETCOREAPP1_1</DefineConstants>
    <Configuration>prod-zone-a</Configuration>
</PropertyGroup>

web.prod-zone-a.config looks like:

<system.webServer>
    <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore>
        <environmentVariables xdt:Transform="Replace">
            <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="prod-zone-a" />
        </environmentVariables>
    </aspNetCore>
</system.webServer>

I tried to run publish by two commands:

dotnet msbuild /t:Publish /p:OutputPath=c:\delivery /p:Configuration=prod-zone-a

and

dotnet publish --configuration prod-zone-a --output c:\delivery

But no transformation applies to web.config on output - just the default value. Do I miss something in configuration or command executing?


Solution

  • 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