63

我有一些存储在数据库中的 JSON 字符串,我想将其作为 JsonResult 返回给客户端。我知道 Json(object) 将对象转换为 JsonResult 但是如果我已经将结果保存在字符串中怎么办?我可以将它转换为 JsonResult

4

1 回答 1

134

您不需要返回 a JsonResult,因为它的工作是将对象序列化为 JSON 字符串。您已经有了 JSON 字符串,所以只需在 ContentResult 中返回它并指定正确的内容类型:

string json = //get some json from your DB
return new ContentResult { Content = json, ContentType = "application/json" };

请记住,您的 MVC 操作方法都应该具有ActionResult作为返回类型,因此您可以ContentResult像返回JsonResult.

于 2010-04-21T18:00:44.003 回答