Friday, December 13, 2013

MVC with [HttpGet] and JsonResult throws exception


For security purposes, MVC prevents json results from being returned with a http get. However, there are instances when that behavior is desired and this is how it's done.




Issue:

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.

The following action is used when the exception is thrown.
[HttpGet]
public JsonResult Get(string type)
{
    return Json(new { test = "testing" });
}


Solution:

Pass JsonRequestBehavior.AllowGet to the Json method that’s returned

return Json(new { test = "testing" }, JsonRequestBehavior.AllowGet);


No comments:

Post a Comment