ASP.NET MVC在发送状态为500的响应时显示错误页面

lan*_*nte 3 c# iis asp.net-mvc json http

我在服务器中有以下操作:

[HttpPost]
public JsonResult SearchContracts(SearchViewModel vm)
{
    List<string> errors;

    if (IsValid(vm, out errors))
    {
        return Json(service.Search(vm), JsonRequestBehavior.AllowGet);
    }
    else
    {
        HttpContext.Response.StatusCode = 500;

        return Json(new { Errors = errors }, JsonRequestBehavior.AllowGet);
    }
}
Run Code Online (Sandbox Code Playgroud)

在本地,它很棒.当请求无效时,它返回带有错误的JSON,并且响应具有500HTTP状态代码.

部署时,IIS将返回我这个着名的错误页面,而不是所描述的JSON .

以下是web.configcustomErrors部分的详细信息:

<customErrors mode="Off" defaultRedirect="~/error.htm">
    <error statusCode="404" redirect="~/errorhandling/pagenotfound" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

我试过把它转到On但不起作用.

我应该在哪里更改以停止接收那个丑陋的错误页面而不是我的美丽JSON?

编辑:

我将状态代码更改为400,现在我将文本Bad Request作为响应,而不是JSON:

请求文本错误

lan*_*nte 7

这个链接:

解决了编辑web.config并添加以下属性:

<system.webServer>
    ...
    <httpErrors existingResponse="PassThrough"></httpErrors>
    ...
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

另一个奇怪的微软故事.