IIS 8.5 覆盖自定义 JSON 错误响应,而不是 return 默认的 500 错误响应页面。如何让 IIS 8.5 出现 return 我的自定义错误?

IIS 8.5 overriding custom JSON error response, instead returns the default 500 error response page. How can I get IIS 8.5 to return my custom error?

我遇到了一个恼人的问题,我认为这是由 IIS 8.5 引起的。使用我正在创建的 Web 应用程序,我创建了一个自定义 JSON HttpStatusResult class(这是一个想法 from this Whosebug post),它使我能够 return JSON 旁边是 500、400 或其他类型的 HTTP 响应状态代码。这是代码:

public class JsonHttpStatusResult : JsonResult
{
    private readonly HttpStatusCode _httpStatus;

    public JsonHttpStatusResult(object data, HttpStatusCode httpStatus)
    {
        Data = data;
        _httpStatus = httpStatus;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.RequestContext.HttpContext.Response.StatusCode = (int)_httpStatus;
        JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        base.ExecuteResult(context);
    }
}

在使用 IISExpress 进行本地测试时,它按我的预期工作 - 如果我 return 500 Http 状态代码以及一些 JSON,响应仅包含 JSON.但是,当我将网站发布到我们拥有的 IIS 8.5 网络服务器时(不幸的是,我们还没有任何 运行 IIS 10 - 我认为这不会有什么不同?)而是 returns 默认的 500 错误响应页面,而不是 JSON:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#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>

所以这就是我遇到的问题 - 如何让 IIS 进入 return 我的自定义 500 JSON 错误结果,而不是这个默认的 500 页面?我环顾四周,看到一些帖子提到需要这两行重要的代码:

HttpContext.Response.Clear();
HttpContext.Response.TrySkipIisCustomErrors = true;

但是,我不知道如何在发送我的自定义错误响应时设置这些,因为有一次我 return 一个 JSON 错误响应,就像在这个例子中:

return new JsonHttpStatusResult(new
{
    ResponseMessage = ex.Message
},
HttpStatusCode.InternalServerError);

我无法设置响应,因为一旦调用 return 就不再运行代码?一旦我的自定义 Json 结果 class 创建了响应,我是否可以通过某种方式连接到响应,我看到了类似 with this post 的东西,但不幸的是它没有似乎解释了我应该如何使用它?

值得一提的是,我尝试 return JSON 错误响应的原因是因为 Ajax 从前端调用了这些操作,然后这些前端 javascript 的错误是 handled/displayed,这就是为什么我要传回的不仅仅是“500 内部服务器错误”消息。

好吧,找到了解决我的问题的方法,我所要做的就是将它添加到 system.webServer 元素中的 web.config 中:

<httpErrors existingResponse="PassThrough"/>

最终不需要更改任何其他设置或修改响应。似乎此设置会导致 IIS 在发生错误时传递现有响应。