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?
source to share
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
source to share
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
source to share
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>
source to share
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 )
- Install package -
dotnet add package DotNet.Xdt --version 2.1.0
- Edit .csproj - add package - see Github
- Edit .csproj - add transform (
ApplyXdtConfigTransform
) code at the end - see Github - Add
web.DEV_Server.config
transformweb.DEV_Server.config
by right clickingDEV_Server.pubxml
- Added the following to
web.DEV_Server.config
<environmentVariable xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Development" xdt:Transform="SetAttributes"/>
- Modify
DEV_Server.pubxml
to change the next parameter value.
<LastUsedBuildConfiguration>DEV_Server</LastUsedBuildConfiguration>
- Check connection and publish
The deployment still loads other configuration files, not knowing how to stop it.
source to share
This worked for me:
- Add the file
web.release.config
to the root directory of the project. - 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>
source to share
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>
source to share