2014-02-21 30 views
15

我自己建立一個JObject並且想把它作爲ActionResult返回。我不想創建並序列化數據對象Json.Net和ActionResult

例如

public ActionResult Test(string id) 
{ 
     var res = new JObject(); 
     JArray array = new JArray(); 
     array.Add("Manual text"); 
     array.Add(new DateTime(2000, 5, 23)); 
     res["id"] = 1; 
     res["result"] = array; 
     return Json(res); //??????? 
} 
+2

@李安這個問題來了,所以它是如何重複?你標記了錯誤的問題。 – Lankymart

回答

28

你應該能夠d這在你的行動方法:

return Content(res.ToString(), "application/json"); 
+1

太棒了,thx很多! – Windys

4

在情況下,如果你照顧JSON格式的,只是return JSON Formatted string

public string Test(string id) 
{ 
     var res = new JObject(); 
     JArray array = new JArray(); 
     array.Add("Manual text"); 
     array.Add(new DateTime(2000, 5, 23)); 
     res["id"] = 1; 
     res["result"] = array; 
     return YourJSONSerializedString; 
} 

其他使用內置的JsonResult( ActionResult)

public JsonResult Test(string id) 
    { 

      return Json(objectToConvert); 
    } 
+0

您需要填寫反應內容類型和其他內容以準備適當的響應嗎?你確定我可以扔串嗎? – Windys