Azure DevOps - .NET Core 构建包含 Web.config 文件

Azure DevOps - .NET Core Build Include Web.config file

我正在使用 Azure DevOps 构建 .NET Core MVC Web 应用程序并将其发布到 AWS 中的 Windows Server 2016 EC2 实例。

我有不同的环境,所以我创建了以下 appsettings.json 个文件:

经过一些研究,我发现我们可以在 web.config 文件中设置 ASPNETCORE_ENVIRONMENT 变量:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\www.MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="[ENV]" />
      </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

然后我可以在 Program.cs 中使用以下代码加载相应的环境 appsetting.json 文件:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(ConfigConfiguration)
                .UseStartup<Startup>();

        static void ConfigConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder config)
        {
            config.SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);

        }
    }

对于每个部署,我想控制部署的 web.config,以便我可以控制 ASPNETCORE_ENVIRONMENT 的值。类似于传统 ASP.NET 环境中的 web.config 转换。

有没有办法在 Azure DevOps 中或通过 Visual Studio 中的设置执行此操作?我读到 .NET Core 2.2 将为此提供解决方案,但在此期间可以做什么?

我正在使用标准 web.config 转换(部署到 IIS)

转型web.staging.config:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables>
          <environmentVariable xdt:Transform="Replace" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Staging" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>