ASP中返回数据的标准方法.NET核心Web Api使用IActionResult
,并提供例如OkObject
的结果.这对对象很管用,但是如果我以某种方式获得了一个JSON字符串,并且只想将该JSON返回给调用者呢?
e.g.
public IActionResult GetSomeJSON()
{
return Ok("{ \"name\":\"John\", \"age\":31, \"city\":\"New York\" }");
}
What ASP.NET Core does here is, it takes the JSON String, and wraps it into JSON again (e.g. it escapes the JSON)
通过提供"原始"内容,返回[Produces("text/plain")]
的纯文本确实有效,但它也将响应的内容类型设置为纯文本,而不是JSON.我们在控制器上使用[Produces("application/json")]
.
如何在不转义的情况下返回作为普通JSON内容类型的JSON?
Note: It doesn't matter how the JSON string was aquired, it could be from a 3rd party service, or there are some special serialization needs so that we want to do custom serialization instead of using the default JSON.NET serializer.