MVC 3 JsonResult has no d property

I am porting old code using .net 3.5, which had some asmx webservices that returned json data. These services returned json wrapped in the "d" property, which was introduced in .net 3.5 for security purposes.

When moving these web services into mvc controller actions, there is no d property that bothers me, since the d property was a security patch introduced for some reason.

Should I transfer the Json result to the d property myself or am I doing something wrong?

public JsonResult GetJsonData() { return Json(2); } 

these outputs:

 2 

instead:

 { "d": "2" } 
+4
source share
2 answers

try it

 public JsonResult GetJsonData() { return Json(new {d = 2}, JsonRequestBehavior.AllowGet); } 
+3
source

You are doing everything right.

I am not one of the developers of MVC developers, but I think that it was decided not to introduce d-wrapper in favor of compatibility with the rest of the world.

However, they took a step towards providing json responses. By default, you cannot return Json in response to a GET request, so you have to add additional conditions to your code:

 public JsonResult GetJsonData() { return Json(2, JsonRequestBehavior.AllowGet); } 

If you want to pass a Json array with sensitive data back to the GET request, then yes, you have to manually wrap the array.

+2
source

Source: https://habr.com/ru/post/1479266/


All Articles