Set web.config transform to Asp.NET Core

I just ran into the problem of converting web.config in asp.net core.

There are two files: base web.config and web.prod-zone-a.config. My goal is to use transform inside web.prod-zone-a.config when publishing my project. I have the following prod-zone-a config 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 this:

<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 start publishing with 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 the transformation is not applied to the web.config in the output - just the default. Missing something in configuration or command execution?

+7


source to share


8 answers


There is a well-documented tool for xdt conversions on github . Also it doesn't depend on the command and it works finedotnet publish

dotnet msbuild



+7


source


In the latest dotnet cli (2.1.400 or higher), you can simply set this msbuild $ (EnvironmentName) property and the publish tool will take care of adding the ASPNETCORE_ENVIRONMENT environmentVariable to the web.config with the environment name specified.

XDT support is also available starting from version 2.2.100-preview1.



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

+3


source


maybe I'm not clear on the question. In my case, web.config overrides all settings in the web.Release.config file.

The fix for me, I just add a link to convert the xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" to configuration

file xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" to configuration

.

So the .config file should run:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

      

After a while, the best solution would be to use the dotnet-transform-xdt tool

+1


source


Following the answer from user1820686, answer above:

The github page skips some of the steps required to add this tool for MSBuild / csproj:

You need to open a command line in your project directory and run dotnet add myProj.csproj package Microsoft.DotNet.Xdt.Tools --version 2.0.0

Then you need to open the csproj file and add

<ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" /> <DotNetCliToolReference Include="Microsoft.Dotnet.Xdt.Tools" Version="2.0.0" /> ... other package references ... </ItemGroup>

+1


source


This is now supported dotnet publish

from SDK version 2.2 with a whole range of dotnet publish

options.

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

I think in the example from the question, it will work when posted as

dotnet publish --configuration prod-zone-a

+1


source


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

Do this first: (link: https://github.com/nil4/dotnet-transform-xdt#-use-with-msbuildcsproj-tooling )

  1. Install package - dotnet add package DotNet.Xdt --version 2.1.0

  2. Edit .csproj - add package - see Github
  3. Edit .csproj - add transform ( ApplyXdtConfigTransform

    ) code at the end - see Github
  4. Add web.DEV_Server.config

    transform web.DEV_Server.config

    by right clickingDEV_Server.pubxml

  5. Added the 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 change the next parameter value.

<LastUsedBuildConfiguration>DEV_Server</LastUsedBuildConfiguration>

  1. Check connection and publish

The deployment still loads other configuration files, not knowing how to stop it.

0


source


This worked for me:

  1. Add the file web.release.config

    to the root directory of the project.
  2. In Visual Studio 2017, publish using Web Deploy (make sure it's installed for release). The settings will be automatically matched.

Conversion example:

<?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>

      

0


source


This worked for me from 1. & 2.above:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <httpErrors existingResponse="PassThrough"
                  xdt:Locator="Match(existingResponse)"
                  xdt:Transform="InsertIfMissing" />
    </system.webServer>
  </location>
</configuration>

      

0


source







All Articles