Set web.config transform in Asp.NET Core

后端 未结 8 1605
遥遥无期
遥遥无期 2021-01-04 10:54

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 t

相关标签:
8条回答
  • 2021-01-04 11:31

    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>
    
    0 讨论(0)
  • 2021-01-04 11:36

    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

    0 讨论(0)
  • 2021-01-04 11:37

    This is now supported by dotnet publish from SDK version 2.2 with a whole bunch of 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 would then work when published as

    dotnet publish --configuration prod-zone-a

    0 讨论(0)
  • 2021-01-04 11:39

    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.

    0 讨论(0)
  • 2021-01-04 11:42

    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>
    
    0 讨论(0)
  • 2021-01-04 11:46

    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>
    
    0 讨论(0)
提交回复
热议问题