Should my MVC controller really know about JSON?

后端 未结 7 1974
有刺的猬
有刺的猬 2021-01-30 14:51

The JsonResult class is a very useful way to return Json as an action to the client via AJAX.

public JsonResult JoinMailingList(string txtEmail)
{
        // ...         


        
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 15:14

    Alternatively, if you don't want to get into using reflection, you can create a RouteValueDictionary with the result's Data property. Going with the OP's data...

    var jsonData = new RouteValueDictionary(result.Data);
    Assert.IsNotNull(jsonData);
    
    Assert.AreEqual(2,
                    jsonData.Keys.Count);
    
    Assert.AreEqual("123",
                    jsonData["foo"]);
    
    Assert.AreEqual(true,
                    jsonData["success"]);
    

提交回复
热议问题