【发布时间】:2017-01-08 21:15:30
【问题描述】:
所以我正在编写一些 Asp.Net WebApi 代码来与没有使用模型类的旧 C# 后端代码挂钩。 (从DataAccess返回的纯dataTable,疯了吧?我知道)
以下是我放在服务器端的代码。
public IHttpActionResult GetProduct(int campaignID, int productID)
{
var so = new SearchOptions(campaignID)
{
ProductID = productID
};
var result = SearchManager.Search(so);
if (result == null || result.Rows.Count == 0)
return NotFound();
return Ok(result.Rows[0]);
}
我希望得到这样的回应:
{
Field1: "field1",
Field2: "field2",
...
}
但实际上我有这个:
{
"rowError": "",
"rowState": 2,
"table": [
{
Field1 : "field1",
Field2 : "field2",
...
}
],
"itemArray": ["field1","field2"],
"hasErrors": false
}
我不想要所有这些rowError、rowState...等
如果我在服务器端这样做:
public IHttpActionResult GetProduct(int campaignID, int productID)
{
var so = new SearchOptions(campaignID)
{
ProductID = productID
};
var result = SearchManager.Search(so);
if (result == null || result.Rows.Count == 0)
return NotFound();
return Ok(result);
}
我收到了这个:
[{Field1: "field1", Field2: "field2"..}]
这很遗憾被ngResource get 方法拒绝,因为它是一个数组而不是单个 Json 对象。
我该怎么办?如果我只想将单个 dataRow 作为 Json 字符串返回。
理想情况下,我想避免走上 Manoz 建议的创建响应对象的路径。 (不过感谢您的回答 Manoz)
谢谢
【问题讨论】:
标签: c# asp.net json asp.net-web-api