Satya
Satya

Reputation: 206

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

Since sometimes my system hits with GET type and some times hit with POST type. While returning Json result shall I use JsonRequestBehavior.AllowGet if I decorate my method with [HttpPost] attribute?

eg:

        [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);
        }

If I write like above in PROD environment will it cause any issues? Please advise.

Upvotes: 1

Views: 2103

Answers (1)

Mathieu VIALES
Mathieu VIALES

Reputation: 4772

Adding the JsonRequestBehavior.AllowGet parameter to your return Json has no use since your method is decorated with [HttpPost] so it can't be called using the GET verb.

You say that sometimes your system "hits with get and sometimes with post" but if you try to call this method using a GET request the routing system will most likely get a 404.

There is no way this method answers a GET request, so adding the JsonRequestBehavior.AllowGet only makes the code less clear.

If your action must be reachabe using POST and GET verbs, it should be decorated with [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] or [AcceptVerbs("Get", "Post")]

Upvotes: 1

Related Questions