在Asp.NET Core中设置web.config转换

17

我刚刚遇到了asp.net core中web.config转换的问题。

有两个文件:基本的web.config和web.prod-zone-a.config。我的目标是在发布项目时使用web.prod-zone-a.config内部的转换。 我在.csproj中拥有以下“prod-zone-a”配置设置:

<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看起来像:

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

我尝试通过两个命令来发布:

dotnet msbuild /t:Publish /p:OutputPath=c:\delivery /p:Configuration=prod-zone-a

并且

dotnet publish --configuration prod-zone-a --output c:\delivery

但是在输出时,web.config文件不会应用任何转换,仅使用默认值。我是否错过了配置或命令执行中的某些内容?

8个回答

15

这对我很有用:

  1. 在项目根目录中添加web.release.config文件。
  2. 在Visual Studio 2017中,使用Web Deploy进行发布(确保设置为Release)。设置将自动被捕捉到。

示例转换:

    <?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>
更新:如果您想在发布时删除 web.config.release 文件和其他文件,只需编辑您的 .csproj 文件并添加类似以下内容的代码:
  <ItemGroup>
    <Content Remove="appsettings.Development.json" />
    <Content Remove="web.release.config" />
  </ItemGroup>
  <ItemGroup>
    <None Include="appsettings.Development.json" />
    <None Include="web.release.config" />
  </ItemGroup>

2
这对我有用,但是我不得不在<configuration>和<system.webServer>标签之间添加一个额外的<location>标签,以匹配项目模板“ASP.NET Core Web Application”的配置格式。 - Wes Grant
1
感谢您提供的示例转换,非常完美! - RobCo
1
谢谢,但是在发布后web.Release.config仍然存在 :( 有什么合法的方法可以摆脱它吗? - Dmitry Gusarov
1
@DmitryGusarov - 我已经添加了一些针对你特定问题的代码。 - Sha
1
如果这不起作用,请确保在csproj文件中将IsTransformWebConfigDisabled设置为false。 - viktorfilim

10

在GitHub上有一个广为人知的工具可以进行XDT转换。 此外,它不依赖于命令,dotnet publishdotnet msbuild都能正常运行。


5
使用最新版本的dotnet cli(2.1.400或更高版本),您只需设置此msbuild属性$(EnvironmentName),发布工具将负责将ASPNETCORE_ENVIRONMENT环境变量添加到web.config中,并指定环境名称。同时,从2.2.100-preview1开始提供XDT支持。示例:https://github.com/vijayrkn/webconfigtransform/blob/master/README.md

1
使用当前版本2.2,可以完美地通过以下方式添加ASPNETCORE_ENVIRONMENT:dotnet publish (projectlocation) /p:EnvironmentName=PreProduction - XaviGuardia

2

在Visual Studio 2017(VS2017)中使用IIS Web Deploy部署ASP.NET Core(2.1)

首先按照以下步骤进行操作:(参考:https://github.com/nil4/dotnet-transform-xdt#-use-with-msbuildcsproj-tooling

  1. 安装包 - dotnet add package DotNet.Xdt --version 2.1.0
  2. 修改 .csproj 文件 - 添加包 - 参考 Github
  3. 修改 .csproj 文件 - 在末尾添加转换代码 (ApplyXdtConfigTransform) - 参考 Github
  4. 通过右键单击 DEV_Server.pubxml,添加 web.DEV_Server.config 转换文件
  5. web.DEV_Server.config 中添加以下内容

<environmentVariable xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Development" xdt:Transform="SetAttributes" />

  1. 修改 DEV_Server.pubxml ,将以下设置值进行修改。

<LastUsedBuildConfiguration>DEV_Server</LastUsedBuildConfiguration>

  1. 验证连接并发布

部署仍会上传其他配置文件,不确定如何停止此操作。


2

接着上面用户1820686的回答:

Github页面漏掉了一些添加MSBuild/csproj工具所需的步骤:

您需要在项目目录中打开命令提示符并运行以下命令:

dotnet add myProj.csproj package Microsoft.DotNet.Xdt.Tools --version 2.0.0

接下来您需要打开csproj文件并添加以下内容:

<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
可能我没有很清楚地表达问题。对于我的情况,web.config覆盖了web.Release.config文件中的所有设置。
对我来说,解决方法是在配置文件中添加转换引用xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
因此,.config文件应该以此开始:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

经过一段时间,最好的解决方案是使用dotnet-transform-xdt工具


1

这对我来说很有效,包括上述的1和2:

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

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接