【问题标题】: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 覆盖自定义 JSON 错误响应,而是返回默认的 500 错误响应页面。如何让 IIS 8.5 返回我的自定义错误?
【发布时间】:2020-11-27 18:48:45
【问题描述】:

我遇到了一个烦人的问题,我认为这是由 IIS 8.5 引起的。通过我正在创建的 Web 应用程序,我创建了一个自定义 JSON HttpStatusResult 类(这是一个想法from this stackoverflow post),它使我能够将 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 进行本地测试时,这符合我的预期 - 如果我返回 500 Http 状态代码以及一些 JSON,则响应仅包含 JSON。但是,当我将站点发布到我们拥有的 IIS 8.5 Web 服务器时(不幸的是,我们还没有任何正在运行的 IIS 10 - 我认为这不会有什么不同吗?)它反而返回默认的 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 返回我的自定义 500 JSON 错误结果,而不是这个默认的 500 页面?我环顾四周,看到一些帖子提到这是需要的两行重要代码:

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

但是,在发送自定义错误响应时,我不知道如何设置这些,因为一旦我返回 JSON 错误响应,如下例所示:

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

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

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc iis iis-8.5


    【解决方案1】:

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

    <httpErrors existingResponse="PassThrough"/>
    

    最终不需要更改任何其他设置,或修改响应。似乎此设置会导致 IIS 在出现错误时通过现有响应。

    【讨论】:

    • 如果您的问题得到解决,我请求您将有用的建议标记为答案。这将帮助面临同样问题的其他人。
    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 2021-01-08
    • 2020-11-16
    • 2015-09-22
    • 2018-05-17
    • 2011-06-30
    • 2014-11-03
    • 2021-11-07
    相关资源
    最近更新 更多