Mengubah web.config

Oleh Vijay Ramakrishnan

Transformasi ke file web.config dapat diterapkan secara otomatis saat aplikasi diterbitkan berdasarkan:

Transformasi ini terjadi untuk salah satu skenario pembuatan web.config berikut:

  • Dihasilkan secara otomatis oleh Microsoft.NET.Sdk.Web SDK.
  • Disediakan oleh pengembang di akar konten aplikasi.

Konfigurasi build

Transformasi konfigurasi build dijalankan terlebih dahulu.

Sertakan web.{ CONFIGURATION}.config file untuk setiap konfigurasi build (Debug|Rilis) memerlukan transformasi web.config .

Dalam contoh berikut, variabel lingkungan khusus konfigurasi diatur di 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>

Transformasi diterapkan saat konfigurasi diatur ke Rilis:

dotnet publish --configuration Release

Properti MSBuild untuk konfigurasi adalah $(Configuration).

Profil

Transformasi profil dijalankan kedua, setelah konfigurasi Build berubah.

Sertakan web.{ PROFILE}.config file untuk setiap konfigurasi profil yang memerlukan transformasi web.config .

Dalam contoh berikut, variabel lingkungan khusus profil diatur di web. FolderProfile.config untuk profil penerbitan folder:

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

Transformasi diterapkan ketika profil adalah FolderProfile:

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

Properti MSBuild untuk nama profil adalah $(PublishProfile).

Jika tidak ada profil yang diteruskan, nama profil defaultnya adalah FileSystem dan web. FileSystem.config diterapkan jika file ada di akar konten aplikasi.

Lingkungan

Transformasi lingkungan dijalankan ketiga, setelah Konfigurasi build dan Transformasi profil.

Sertakan web.{ ENVIRONMENT}.config file untuk setiap lingkungan yang memerlukan transformasi web.config .

Dalam contoh berikut, variabel lingkungan khusus lingkungan diatur di web. Production.config untuk lingkungan Produksi:

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

Transformasi diterapkan ketika lingkungan adalah Produksi:

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

Properti MSBuild untuk lingkungan adalah $(EnvironmentName).

Saat menerbitkan dari Visual Studio dan menggunakan profil penerbitan, lihat Visual Studio menerbitkan profil (.pubxml) untuk penyebaran aplikasi ASP.NET Core.

Variabel ASPNETCORE_ENVIRONMENT lingkungan secara otomatis ditambahkan ke file web.config saat nama lingkungan ditentukan.

Adat

Transformasi kustom dijalankan terakhir, setelah Konfigurasi build, Profil, dan Transformasi Lingkungan .

Sertakan file {CUSTOM_NAME}.transform untuk setiap konfigurasi kustom yang memerlukan transformasi web.config.

Dalam contoh berikut, variabel lingkungan transformasi kustom diatur dalam 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>

Transformasi diterapkan ketika CustomTransformFileName properti diteruskan ke perintah penerbitan dotnet:

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

Properti MSBuild untuk nama profil adalah $(CustomTransformFileName).

Mencegah transformasi web.config

Untuk mencegah transformasi file web.config , atur properti $(IsWebConfigTransformDisabled)MSBuild :

dotnet publish /p:IsWebConfigTransformDisabled=true

Sumber Daya Tambahan: