我们可以将 JsonRequestBehavior.AllowGet 与 [HttpPost] 属性一起使用吗?

Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?

因为有时我的系统命中 GET 类型,有时命中 POST 类型。 在返回 Json 结果时,如果我用 [HttpPost] 属性装饰我的方法,我应该使用 JsonRequestBehavior.AllowGet 吗?

例如:

        [HttpPost, ValidateAntiForgeryToken, Authorize]
        public ActionResult AssociatedDevices(long id, [DataSourceRequest] DataSourceRequest request)
        {
            var dataParameters = request.ToDataParameters();
            var deviceSetLogic = new DeviceSetLogic();
            var associatedDevices = deviceSetLogic.GetAssociatedDevicesByDeviceSetId(id, dataParameters);

            var result = new DataSourceResult()
            {
                Data = associatedDevices,
                Total = Convert.ToInt32(dataParameters.TotalRecordCount)
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }

如果我在PROD环境下像上面这样写,会不会有什么问题?请指教

JsonRequestBehavior.AllowGet 参数添加到 return Json 没有用,因为您的方法用 [HttpPost] 装饰,因此无法使用 GET 动词调用.

您说有时您的系统 "hits with get and sometimes with post" 但如果您尝试使用 GET 请求调用此方法,路由系统很可能会收到 404。

此方法无法回答 GET 请求,因此添加 JsonRequestBehavior.AllowGet 只会使代码不那么清晰。

如果您的操作必须使用 POST 和 GET 动词到达,则应使用 [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)][AcceptVerbs("Get", "Post")]

修饰