【问题标题】:IIS returning default Error HTML instead of JSON, as instructed按照指示,IIS 返回默认错误 HTML 而不是 JSON
【发布时间】:2019-06-27 21:48:04
【问题描述】:

我为我的 ASP.NET MVC 项目中的某些端点创建了一个自定义 Attribute,它指示服务器返回一个 JSON 对象,而不是按通常的方式处理错误。属性如下所示:

public class AjaxErrorHandler : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            filterContext.HttpContext.Response.StatusDescription =
                filterContext.Exception.Message.Replace('\r', ' ').Replace('\n', ' ');
            filterContext.Result = new JsonResult
            {
                Data = new { errorMessage = filterContext.Exception.Message }
            };
        }
    }
}

每当我在本地调试解决方案时,它都能正常工作,并在出错时返回以下内容:

{"errorMessage":"Error message goes here"}

但是当我将解决方案部署到我的生产服务器时,服务器始终返回以下 HTML:

 #content{margin:0 0 0 2%;position:relative;}
 .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
 -->
 </style>
 </head>
 <body>
 <div id = "header" >< h1 > Server Error</h1></div>
 <div id = "content" >
     < div class="content-container"><fieldset>
 <h2>500 - Internal server error.</h2>
 <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
 </div>
 </body>
 </html>

我这里缺少什么 web 项目的配置,以使服务器遵守返回 JSON 对象的指令?

【问题讨论】:

  • 检查本地和生产机器之间的 web.config 设置。您发现某些差异了吗?
  • 相同的配置文件。唯一不同的是数据库的连接字符串

标签: asp.net-mvc iis asp.net-mvc-5


【解决方案1】:

我以前在 IIS 上见过这种情况。

凭记忆,你可以试试这个:

  • 打开 IIS 管理器
  • 选择相关网站
  • 双击错误页面图标
  • 点击右侧的编辑功能设置
  • 将设置更改为远程请求的本地错误页面和自定义错误页面的详细错误
  • 再次尝试该网站

认为这就是我过去的解决方法

【讨论】:

    猜你喜欢
    • 2014-11-03
    • 1970-01-01
    • 2013-04-16
    • 2018-03-03
    • 2019-06-22
    • 1970-01-01
    • 2012-05-27
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多