我们将JSON配置数据存储在数据库中。我需要获取这个JSON数据,并通过asp.net web-api将其返回到浏览器:
public class ConfigurationController : ApiController
{
public string Get(string configId)
{
// Get json from database
// when i return the fetched json it get's escaped
}
}返回的值将被转义。如何简单地返回字符串的原样?我真的不想填充被序列化为JSON的对象。
发布于 2014-03-27 13:44:35
您可以从HttpResponseMessage返回一个ApiController,它允许您基本上返回一个字符串值。
例如:
public HttpResponseMessage Get()
{
return new HttpResponseMessage() {
Content = new StringContent("Hello World", System.Text.Encoding.UTF8, "application/json")
};
}您想要做的就是将json字符串作为StringContent传递。
发布于 2017-01-11 18:22:25
已被接受的答案略为偏离。正确的版本是:
public HttpResponseMessage Get()
{
return new HttpResponseMessage() {
Content = new StringContent("Hello World", System.Text.Encoding.UTF8, "application/json")
};
}正确删除转义的是StringContent函数。如果没有应用程序/json媒体类型,像postman这样的工具将无法正确显示结果的“漂亮”版本。
发布于 2015-04-06 15:04:51
这对我起了作用:
return Request.CreateResponse( HttpStatusCode.OK, stringContent );https://stackoverflow.com/questions/22689328
复制相似问题