Transform web.config (Chuyển đổi web.config)
Nguồn: Transform web.config
Các transformation (chuyển đổi) đối với file web.config có thể được áp dụng tự động khi ứng dụng được publish dựa trên:
- Build configuration (cấu hình build)
- Profile (hồ sơ)
- Environment (môi trường)
- Custom (tùy chỉnh)
Các transformation này xảy ra cho một trong các tình huống tạo web.config sau:
- Được tạo tự động bởi SDK
Microsoft.NET.Sdk.Web. - Được cung cấp bởi developer trong content root của ứng dụng.
Build configuration
Build configuration transform được chạy đầu tiên.
Bao gồm file web.{CONFIGURATION}.config cho mỗi build configuration (Debug|Release) yêu cầu transformation web.config.
Trong ví dụ sau, một biến môi trường cụ thể theo cấu hình được đặt trong 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>Transformation được áp dụng khi configuration được đặt thành Release:
dotnet publish --configuration Release
MSBuild property cho configuration là $(Configuration).
Profile
Profile transform được chạy thứ hai, sau Build configuration transform.
Bao gồm file web.{PROFILE}.config cho mỗi cấu hình profile yêu cầu transformation web.config.
Trong ví dụ sau, một biến môi trường cụ thể theo profile được đặt trong web.FolderProfile.config cho folder publish profile:
<?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>Transformation được áp dụng khi profile là FolderProfile:
dotnet publish --configuration Release /p:PublishProfile=FolderProfile
MSBuild property cho tên profile là $(PublishProfile).
Nếu không có profile nào được truyền vào, tên profile mặc định là FileSystem và web.FileSystem.config được áp dụng nếu file tồn tại trong content root của ứng dụng.
Environment (Môi trường)
Environment transform được chạy thứ ba, sau Build configuration và Profile transform.
Bao gồm file web.{ENVIRONMENT}.config cho mỗi environment yêu cầu transformation web.config.
Trong ví dụ sau, một biến môi trường cụ thể theo môi trường được đặt trong web.Production.config cho môi trường 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>Transformation được áp dụng khi môi trường là Production:
dotnet publish --configuration Release /p:EnvironmentName=Production
MSBuild property cho môi trường là $(EnvironmentName).
Khi publish từ Visual Studio và sử dụng publish profile, xem Visual Studio publish profiles (.pubxml) for ASP.NET Core app deployment.
Biến môi trường ASPNETCORE_ENVIRONMENT được tự động thêm vào file web.config khi tên môi trường được chỉ định.
Custom (Tùy chỉnh)
Custom transform được chạy cuối cùng, sau Build configuration, Profile, và Environment transform.
Bao gồm file {CUSTOM\_NAME}.transform cho mỗi cấu hình tùy chỉnh yêu cầu transformation web.config.
Trong ví dụ sau, một biến môi trường custom transform được đặt trong 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>Transformation được áp dụng khi thuộc tính CustomTransformFileName được truyền vào lệnh dotnet publish:
dotnet publish --configuration Release /p:CustomTransformFileName=custom.transform
MSBuild property cho tên profile là $(CustomTransformFileName).
Ngăn chặn transformation web.config
Để ngăn chặn các transformation của file web.config, hãy đặt MSBuild property $(IsWebConfigTransformDisabled):
dotnet publish /p:IsWebConfigTransformDisabled=true