IIS部署.NET4.7+ C# REST API报解析实体名称错误(第5行第51位)
报错信息
IIS - 网站 - 浏览 - 解析错误:解析实体名称时出错,行5位置51。
问题背景
- 基于.NET 4.7+开发C# REST API;
- API通过Visual Studio完成发布;
- API以IIS服务形式安装部署;
- API作为Swagger服务运行,可通过Postman调用调试;
- 已在多台Windows Server 2016设备上测试运行正常;
- 某台安装相同.NET版本的Windows Server 2016设备上无法运行,持续抛出上述错误。
已尝试排查操作
- 删除原疑似有问题的web.config段落(该段落在其他设备上运行正常),仍会报相同的实体名称解析错误,位置仍为第5行第51位;调整web.config对应行内容后,问题依旧复现;
- 尝试删除web.config中部分实体;
- 尝试重装IIS;
- 尝试查看事件查看器日志及IIS日志,均未定位到问题原因。
web.config完整代码
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" /> </configSections> <appSettings file="..\..\AppSecrets.config"> <add key="cors" value="localhost;localhost2; ;" /> <add key="connection" value="web" /> <add key="waitandretry" value="3" /> <add key="circuitbreaker" value="3" /> <add key="circuitbreakertime" value="5" /> <add key="apikey" value="xxxxxx" /> </appSettings> <common> <logging> <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog4411"> <arg key="configType" value="INLINE" /> </factoryAdapter> </logging> </common> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="C:\inetpub\wwwroot\sms\MyAPI\logs\InnerLog.txt" internalLogLevel="Error" autoReload="true"> <extensions> <add assembly="NLog.WindowsEventLog" /> </extensions> <targets> <target xsi:type="File" name="log" fileName="${basedir}\logs\${date:format=yyyy-MM-dd}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> <target xsi:type="EventLog" name="eventlog" source="testlogging" log="Application" layout="${message}${newline}${exception:format=ToString}" /> </targets> <rules> <logger name="*" minlevel="Error" writeTo="log" /> <logger name="*" minLevel="Error" writeTo="eventlog" /> </rules> </nlog> <system.web> <trace enabled="true" /> <compilation targetFramework="4.7.2" /> <httpRuntime targetFramework="4.6.1" /> </system.web> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="xxxxxx" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="xxxxxx" /> <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="xxxxx" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="xxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Owin" publicKeyToken="xxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Cors" publicKeyToken="xxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Http" publicKeyToken="xxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="xxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Http.Owin" publicKeyToken="xxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Autofac" publicKeyToken="xxxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.8.1.0" newVersion="4.8.1.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Autofac.Integration.WebApi" publicKeyToken="xxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Autofac.Integration.Owin" publicKeyToken="xxxxx" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type=" Microsoft.CodeDom.Providers.DotNetCompilerPlatform. CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=xxxxx" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type= "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=xxxx" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> <system.webServer> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> <defaultDocument> <files> <add value="/swagger" /> </files> </defaultDocument> </system.webServer> </configuration>
问题原因及修复方案
该报错本质是XML解析失败,修改web.config后仍报相同位置错误,说明IIS解析的配置文件不是你修改的目标文件,或是存在其他层级的配置文件出错,按以下优先级排查即可:
- 先确认IIS站点配置的物理路径是否正确,确保你修改的web.config和站点指向路径下的文件为同一个
- 排查站点引用的外部配置文件
..\..\AppSecrets.config,该文件内容会被合并到web.config解析流程中,检查该文件第5行是否存在未转义的&、<等XML非法字符,或是实体名称定义错误 - 检查父级目录的配置文件,包括站点上层目录的web.config、.NET Framework全局配置文件
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config和machine.config,排查这些文件第5行是否存在语法错误 - 确认IIS站点应用程序池的.NET CLR版本配置为v4.0,避免低版本CLR解析高版本配置文件触发异常
内容的提问来源于stack exchange,提问作者Franco
相关产品推荐
相关产品推荐

