关于 IIS 上运行 ASP.NET Core 站点的“HTTP 错误 500.19”错误

昨天回答了博问中的一个问题 —— “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 的安装方面浪费了不少时间。

posted @ 2019-05-04 09:55  dudu  阅读(18558)  评论(8编辑  收藏  举报