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)
- Install package -
dotnet add package DotNet.Xdt --version 2.1.0
- Modify .csproj - add package - refer github
- Modify .csproj - add transform code (
ApplyXdtConfigTransform
) at the end - refer github - Add
web.DEV_Server.config
transfor file by right-clicking onDEV_Server.pubxml
- Added following to
web.DEV_Server.config
<environmentVariable xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Development" xdt:Transform="SetAttributes" />
- Modify
DEV_Server.pubxml
to modify following setting value.
<LastUsedBuildConfiguration>DEV_Server</LastUsedBuildConfiguration>
- Validate Connection & Publish
Deploy still uploads other config files, not sure how to stop that.
This worked for me:
- Add
web.release.config
file to the project root. - 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>