【问题标题】:MVC JSON response with bad request带有错误请求的 MVC JSON 响应
【发布时间】:2016-07-08 17:21:55
【问题描述】:

在开发 ASP.NET MVC 4 应用程序期间,我在控制器操作中遇到了以 JSON 格式返回通知的问题。下面我附上了我用来发送通知的代码:

return GetNotificationResult(HttpStatusCode.BadRequest, notification);

private JsonResult GetNotificationResult(HttpStatusCode code, string notification)
    {
        Response.StatusCode = (int) code;
        return Json(new { Notification = notification });
    }

当我在 localhost 上测试它时,它按预期工作(它返回状态代码 BadRequest 并且正文包含带有通知的 JSON)。

但是,当我在 Azure 中将此代码部署为 Web 应用程序时,它只返回 BadRequest 而没有 JSON 通知。

在云和本地主机上调用此代码时,什么会导致不同的结果?

【问题讨论】:

  • 尝试将此添加到您的web.config<system.webServer> <httpErrors existingResponse="PassThrough"/> </system.webServer>
  • @Igor 成功了。谢谢!
  • 好交易。我把它写在下面作为答案,如果它有帮助,请使用它旁边的复选框标记它。

标签: asp.net json asp.net-mvc


【解决方案1】:

您可以将以下内容添加到您的 web.config

<system.webServer>
  <httpErrors existingResponse="PassThrough"/>
</system.webServer>

httpErrors 上的文档

PassThrough:如果存在现有响应,则保持响应不变。

【讨论】:

    猜你喜欢
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    相关资源
    最近更新 更多