Pete
Pete

Reputation: 73

MVC 3 JsonResult does not have the d property

I'm migrating old code using .net 3.5 which had some asmx webservices which returned json data. These services returned the json wrapped on a "d" property which was introduced on .net 3.5 for security purposes.

When moving these webservices to mvc controller actions, there is no d property which concerns me as the "d" property was a security fix introduced for a reason.

Should I wrap the Json result on a d property myself or am I doing something wrong?

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

this outputs:

2

instead of:

{ "d": "2" }

Upvotes: 4

Views: 764

Answers (2)

Satpal
Satpal

Reputation: 133433

Try this

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

Upvotes: 3

Dima
Dima

Reputation: 6741

You do everything right.

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

However, they made a step towards securing json responses. By default, you cannot return Json in response to GET request, so you'll have to put extra condition in your code :

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

If you want to serve Json array with sensitive data back to GET request, then yes, you'll have to wrap your array manually.

Upvotes: 2

Related Questions