我有一个 JSON 方法,它接受 GET 请求并返回一个 JSON 对象(不是数组)。我知道 JSON 劫持及其影响。我读过Phil Haack post 。问题是该方法在 98% 的情况下都适用于 GET 和 POST。其他时候我记录这个错误:
This request has been blocked because sensitive information could be disclosed to
third party web sites when this is used in a GET request. To allow GET requests, set
JsonRequestBehavior to AllowGet.
我的方法很简单,只需要一个整数参数...
[Authorize]
public ActionResult MyMediaJSON(int? id) {
<get data & return result>
}
什么条件触发该消息?当我调试这个时应该寻找什么?
最佳答案
我刚刚查看了 MVC 源代码,它与您在问题中所说的内容不符。
对我来说,默认情况下,JsonRequestBehavior.DenyGet
用于所有 JSON 结果。因此,每次尝试使用 GET 请求从 Controller 返回 JSON 时,您都应该收到错误消息(不指定 JsonRequestBehavior.AllowGet
)。
实际控制在JsonResult.ExecuteResult
中完成,如下所示:
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
}
What conditions trigger the message? What should look for as I debug this?
通过 GET 调用且返回 JsonResult
且未指定 JsonRequestBehavior.AllowGet
的任何操作。 ( Controller 中的 Json
方法使用 JsonResult
)
关于asp.net-mvc - 是什么触发了 MVC JSON Get 阻塞 - 间歇性工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6279884/