Set web.config transform in Asp.NET Core
Asked Answered
T

8

18

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?

Tirzah answered 29/3, 2017 at 19:59 Comment(0)
T
11

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

Tirzah answered 30/3, 2017 at 4:12 Comment(0)
D
15

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>
Dormancy answered 15/2, 2019 at 3:45 Comment(5)
This worked for me, but I had to add an additional <location> tag in between <configuration> and <system.webServer> tags to match the config format for project template "ASP.NET Core Web Application".Trencher
Thank you for the sample transformation, works like a charm!Elda
Thanks, but web.Release.config is still there after publish :( any legit way to get rid of it?Amortize
@DmitryGusarov - I have added some code for your specific problem.Dormancy
Be sure that IsTransformWebConfigDisabled is set to false in csproj file if this does not work.Lightening
T
11

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

Tirzah answered 30/3, 2017 at 4:12 Comment(0)
J
5

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

Jiggle answered 24/8, 2018 at 22:3 Comment(1)
With the current version 2.2 works perfectly to add ASPNETCORE_ENVIRONMENT using:dotnet publish (projectlocation) /p:EnvironmentName=PreProductionSorel
P
2

Following on from user1820686's answer above:

The github page misses out some of the steps required to add this for MSBuild/csproj tooling:

You need to open a command prompt 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>
Palestine answered 12/4, 2018 at 15:0 Comment(0)
C
2

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.

Crutchfield answered 27/8, 2018 at 4:48 Comment(0)
P
1

may be i don't clear question. For mine case web.config override all settings in web.Release.config file.

Fix for me, i just add reference for transformation xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" to configuration file.

so, .config file should start:

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

After some time, the best solutions is using dotnet-transform-xdt tool

Paniagua answered 27/6, 2017 at 7:2 Comment(0)
P
1

This is now supported by dotnet publish from SDK version 2.2 with a whole bunch of options.

https://learn.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 would then work when published as

dotnet publish --configuration prod-zone-a

Persona answered 1/5, 2019 at 11:38 Comment(0)
K
1

This worked for me with the 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>
Kesha answered 1/7, 2019 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.