30

我的 ASP.NET MVC 控制器中有一个操作,当将无效参数传递给操作时,它会返回带有 400 Bad Request 的 JSON 数据。

[HttpDelete]
public ActionResult RemoveObject(string id) {
    if(!Validate(id)) {

        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(new { message = "Failed", description = "More details of failure" });
    }
}

这可以在 IIS 下完美运行,或者与从 Visual Studio 启动的开发测试服务器一起运行。将项目部署到 Azure 后,400 Bad Request 返回时没有 JSON 数据。消息的内容类型已更改为“文本/html”和“错误请求”。

为什么 Azure 下的行为不同?

4

1 回答 1

63

将以下条目添加到您的“web.config”中。

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

这将允许 HTTP 错误不受干扰地通过。

于 2013-03-20T19:15:03.013 回答