如何删除 Nancy.Response.AsJson 中的任务 json 属性
How to remove Task json properties in Nancy.Response.AsJson
我已经将我的一个 API 端点和内部逻辑设为异步,而当我以前使用 Response.AsJson(Foo.bar()) 时,它会 return 通常 json 表示,但现在我看到它附加了这个:
{
"result": [
{
"id": "59d680cc734d1d08b4e6c89c",
"properties": {
"name": "value"
}
}
],
"id": 3,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false
}
但我希望它是这样的:
"id": "59d680cc734d1d08b4e6c89c",
"properties": {
"name": "value"
}
据我所知,这是因为我将我的对象包装在一个 Task 中,但我无法弄清楚,我使用 Response.AsJson 的 Nancy 框架如何使它成为属性被排除在外。我显然可以省略 returned 对象的 Response.AsJson,但是如果通过网络浏览器请求,则响应不再是 Json。
进一步举例
用于路由的 NancyModule API:
public ItemCatalogModule(IItemCatalog itemCatalog) : base("/itemCatalog")
{
Get("/fetch/{id}", async parameters =>
{
var id = (string) parameters.id;
var response = await Response.AsJson(itemCatalog.GetItem(id));
return response;
});
}
ItemCatalog 的界面:
public interface IItemCatalog
{
Task<Item> GetItem(string id);
}
你应该这样做:
public ItemCatalogModule(IItemCatalog itemCatalog) : base("/itemCatalog")
{
Get("/fetch/{id}", async parameters =>
{
var id = (string) parameters.id;
return Response.AsJson(await itemCatalog.GetItem(id));
});
}
我已经将我的一个 API 端点和内部逻辑设为异步,而当我以前使用 Response.AsJson(Foo.bar()) 时,它会 return 通常 json 表示,但现在我看到它附加了这个:
{
"result": [
{
"id": "59d680cc734d1d08b4e6c89c",
"properties": {
"name": "value"
}
}
],
"id": 3,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false
}
但我希望它是这样的:
"id": "59d680cc734d1d08b4e6c89c",
"properties": {
"name": "value"
}
据我所知,这是因为我将我的对象包装在一个 Task 中,但我无法弄清楚,我使用 Response.AsJson 的 Nancy 框架如何使它成为属性被排除在外。我显然可以省略 returned 对象的 Response.AsJson,但是如果通过网络浏览器请求,则响应不再是 Json。
进一步举例
用于路由的 NancyModule API:
public ItemCatalogModule(IItemCatalog itemCatalog) : base("/itemCatalog")
{
Get("/fetch/{id}", async parameters =>
{
var id = (string) parameters.id;
var response = await Response.AsJson(itemCatalog.GetItem(id));
return response;
});
}
ItemCatalog 的界面:
public interface IItemCatalog
{
Task<Item> GetItem(string id);
}
你应该这样做:
public ItemCatalogModule(IItemCatalog itemCatalog) : base("/itemCatalog")
{
Get("/fetch/{id}", async parameters =>
{
var id = (string) parameters.id;
return Response.AsJson(await itemCatalog.GetItem(id));
});
}