關於 IIS 上執行 ASP.NET Core 站點的“HTTP 錯誤 500.19”錯誤

dudu發表於2019-05-04

昨天回答了博問中的一個問題 —— “HTTP 錯誤 500.19 - Internal Server Error dotnetcore”,今天在這篇隨筆中時候事後諸葛亮地小結一下。

伺服器是 Windows Server 2008 R2 ,ASP.NET Core 版本是 2.1 ,錯誤資訊如下:

HTTP 錯誤 500.19 - Internal Server Error
無法訪問請求的頁面,因為該頁的相關配置資料無效。

出現這個錯誤是由於 IIS 無法解析 Web.config 中的 xml 配置節點 aspNetCore

<aspNetCore processPath="dotnet" arguments=".\Cnblogs.WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />

出現這個問題通常是由於沒有安裝 ASP.NET Core Module ,只要下載安裝 Hosting Bundle (比如 Microsoft .NET Core 2.1.6 - Windows Server Hosting)就能解決。

博問中的這個問題奇怪之處是已經安裝了 Hosting Bundle ,%windir%\System32\inetsrv 檔案中也有 aspnetcore.dll 這個檔案,可能是安裝過程中出了什麼差錯,沒有成功配置 IIS 。而且解除安裝並重新安裝 Hosting Bundle 也不能解決問題,可能是安裝程式認為 IIS 已經配置好,安裝時沒有重新配置 IIS 。 

只能目測檢查並手動修復 IIS 的相關配置檔案。

對於 HTTP Error 500.19 ,先檢查 %windir%\System32\inetsrv\config\schema 資料夾中有沒有 aspnetcore_schema.xml 檔案,有。

再檢查 %windir%\System32\inetsrv\config\applicationHost.config 中有沒有 aspNetCore section ,沒有,加上:

<section name="aspNetCore" overrideModeDefault="Allow" />

"HTTP Error 500.19" 的問題搞定!

現在的錯誤變成了“HTTP 錯誤 500.21”:

HTTP 錯誤 500.21 - Internal Server Error
處理程式“aspNetCore”在其模組列表中有一個錯誤模組“AspNetCoreModule"

這是由於 Web.config 中配置了 AspNetCoreModule ,但無法載入

<handlers>
  <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>

也需要手工修改 IIS 的配置檔案來解決。

在 %windir%\System32\inetsrv\config\applicationHost.config 中新增2個配置

1)在 globalModules 中新增

<add name="AspNetCoreModule" image="%SystemRoot%\system32\inetsrv\aspnetcore.dll" />

2)在 modules 在新增

<add name="AspNetCoreModule" />

如果新增上面2個配置後還沒解決,在 IIS 站點的“模組”中,點選“配置本機模組”,然後選中 AspNetCoreModule 。

就這樣搞定了這個問題!

小結寫起來容易沉著冷靜,但排查問題時容易急於求成,昨天在折騰 ASP.NET Core 與 Hosting Bundle 的安裝方面浪費了不少時間。

相關文章