web.config を変換する

作成者: Vijay Ramakrishnan

次のものに基づいてアプリを発行する場合、web.config ファイルに対する変換を自動的に適用することができます。

これらの変換は、次のいずれかの web.config の生成シナリオで発生します。

  • Microsoft.NET.Sdk.Web SDK によって自動的に生成された。
  • 開発者によってアプリのコンテンツ ルートに提供された。

[ビルド構成]

ビルド構成の変換は、最初に実行されます。

web.config の変換を必要とするビルド構成 (デバッグ|リリース) ごとに、web.{CONFIGURATION}.config ファイルを含めます。

次の例では、構成固有の環境変数が web.Release.config 内で設定されています。

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables xdt:Transform="InsertIfMissing">
          <environmentVariable name="Configuration_Specific" 
                               value="Configuration_Specific_Value" 
                               xdt:Locator="Match(name)" 
                               xdt:Transform="InsertIfMissing" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

構成が Release に設定された場合、変換が適用されます。

dotnet publish --configuration Release

構成の MSBuild プロパティは $(Configuration) です。

Profile

プロファイルの変換は、ビルド構成の変換の後、2 番目に実行されます。

web.config の変換を必要とするプロファイル構成ごとに、web.{PROFILE}.config ファイルを含めます。

次の例では、プロファイル固有の環境変数が web.FolderProfile.config 内でフォルダーの発行プロファイルに対して設定されています。

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables xdt:Transform="InsertIfMissing">
          <environmentVariable name="Profile_Specific" 
                               value="Profile_Specific_Value" 
                               xdt:Locator="Match(name)" 
                               xdt:Transform="InsertIfMissing" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

プロファイルが FolderProfile であった場合、変換が適用されます。

dotnet publish --configuration Release /p:PublishProfile=FolderProfile

プロファイル名の MSBuild プロパティは $(PublishProfile) です。

プロファイルが渡されなかった場合、既定のプロファイル名は FileSystem です。また、アプリのコンテンツのルートにそのファイルが存在していた場合、web.FileSystem.config が適用されます。

環境

環境の変換は、ビルド構成およびプロファイルの変換の後、3 番目に実行されます。

web.config の変換を必要とする環境ごとに、web.{ENVIRONMENT}.config ファイルを含めます。

次の例では、環境固有の環境変数が web.Production.config 内で Production 環境に対して設定されています。

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables xdt:Transform="InsertIfMissing">
          <environmentVariable name="Environment_Specific" 
                               value="Environment_Specific_Value" 
                               xdt:Locator="Match(name)" 
                               xdt:Transform="InsertIfMissing" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

環境が Production だった場合、変換が適用されます。

dotnet publish --configuration Release /p:EnvironmentName=Production

環境の MSBuild プロパティは $(EnvironmentName) です。

Visual Studio から発行プロファイルを使用して発行する場合は、「ASP.NET Core アプリを配置するための Visual Studio 発行プロファイル (.pubxml)」を参照してください。

環境名を指定すると、環境変数 ASPNETCORE_ENVIRONMENT が自動的に web.config ファイルに追加されます。

カスタム

カスタム変換は、ビルド構成プロファイル、および環境の変換の後、最後に実行されます。

web.config の変換を必要とするカスタム構成ごとに、 {CUSTOM_NAME}.transform ファイルを含めます。

次の例では、カスタム変換の環境変数が custom.transform 内で設定されています。

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables xdt:Transform="InsertIfMissing">
          <environmentVariable name="Custom_Specific" 
                               value="Custom_Specific_Value" 
                               xdt:Locator="Match(name)" 
                               xdt:Transform="InsertIfMissing" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

CustomTransformFileName プロパティが dotnet publish コマンドに渡された場合、変換が適用されます。

dotnet publish --configuration Release /p:CustomTransformFileName=custom.transform

プロファイル名の MSBuild プロパティは $(CustomTransformFileName) です。

web.config の変換を回避する

web.config ファイルの変換を回避するには、MSBuild プロパティ $(IsWebConfigTransformDisabled) を設定します。

dotnet publish /p:IsWebConfigTransformDisabled=true

その他の技術情報