You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC出现HTTP错误500.19时如何重定向错误页或隐藏配置源

解决方案

500.19属于IIS层面的配置解析错误,尚未进入ASP.NET应用管道,需要同时配置IIS级和应用级的错误规则避免信息泄露:

1. 配置IIS自定义错误页(优先配置,针对500.19类IIS错误)

修改web.config的system.webServer节点,关闭详细错误输出,绑定自定义错误页:

<system.webServer>
  <httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace">
    <!-- 专门针对500.19配置错误的跳转规则 -->
    <remove statusCode="500" subStatusCode="19" />
    <error statusCode="500" subStatusCode="19" path="/error/config.html" responseMode="File" />
    <!-- 通用500错误兜底规则 -->
    <remove statusCode="500" />
    <error statusCode="500" path="/error/server.html" responseMode="File" />
  </httpErrors>
</system.webServer>

参数说明:

  • errorMode="DetailedLocalOnly":本地调试时可查看完整错误详情,线上用户仅展示自定义错误页,是生产环境的推荐配置;如果需要全量屏蔽详细错误,可改为errorMode="Custom"
  • responseMode="File":直接返回静态错误页文件,无需经过应用路由,避免二次错误

2. 配置ASP.NET应用级错误兜底

针对可进入应用管道的运行时错误,补充配置system.web节点的自定义错误规则:

<system.web>
  <customErrors mode="RemoteOnly" defaultRedirect="/error/index">
    <error statusCode="404" redirect="/error/notfound" />
    <error statusCode="500" redirect="/error/server" />
  </customErrors>
</system.web>

mode="RemoteOnly"逻辑和上述IIS配置一致,本地可看详情,线上走自定义跳转。

3. 从根源避免配置解析错误

本次触发错误的根因是web.config为XML格式,&属于XML保留转义字符,直接写入会导致XML解析失败。配置连接字符串时将&转义为&amp;即可避免该错误触发,示例:
原错误写法:
connectionString="Server=xxx;Database=xxx;Uid=xxx;Pwd=123&abc;"
修正后写法:
connectionString="Server=xxx;Database=xxx;Uid=xxx;Pwd=123&amp;abc;"


内容的提问来源于stack exchange,提问作者cemil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 14:18:05