"dynamic" keyword and JSON data "dynamic" keyword and JSON data json json

"dynamic" keyword and JSON data


You don't really need dynamic. Here's an example. Suppose you had the following action which you would like to unit test:

public ActionResult Index(){    return Json(new { Id = 5, Foo = "bar" });}

and the corresponding test:

// actvar actual = subjectUnderTest.Index();// assertvar data = new RouteValueDictionary(actual.Data);Assert.AreEqual(5, data["Id"]);Assert.AreEqual("bar", data["Foo"]);

Also you might find the following blog post useful.


The Data property of the JsonResult is of type Object this means, although you have a dynamic declaration, the type that is set is still Object. The other issue is that you are using an anonymous type as the Data and then trying to access that as a declared instance outside of its applicable scope. Use @Darin's technique for accessing the property values using a RouteValueDictionary.