[DotnetCore] web.config 發布控制

9 mins.
  1. 1. 關閉輸出
  2. 2. 建立 web.config
  3. 3. Reference

Dotnet Core 在發布的時候預設會輸出 web.config,發佈到 IIS 伺服器如果有要調整 web.config 的內容,可以用以下幾種做法來達成。

關閉輸出

在 server 上修改,不要讓重新佈署的時候被覆蓋,可以使用 IsTransformWebConfigDisabled 來關閉輸出。

  • 使用指令
1
dotnet publish -c Release -p:IsTransformWebConfigDisabled=true
  • 專案檔 csproj 設定
1
2
3
4
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup>

建立 web.config

如果是希望每個發布的 web.config 都有同樣的內容,那也可以建立 web.config 在專案中。
標準沒有什麼設定的情況下,輸出的檔案內容如下

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Server.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>

增加一個環境變數在檔案內,可以只設定額外增加的內容,剩下的系統會自動幫你填上

1
2
3
4
5
6
7
8
9
10
11
<configuration>
<location>
<system.webServer>
<aspNetCore>
<environmentVariables>
<environmentVariable name="Configuration_Specific" value="Configuration_Specific_Value" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>

上面的內容在執行 publish 指令後會是長這樣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Cimes.Server.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="Configuration_Specific" value="Configuration_Specific_Value" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</location>
</configuration>

Reference