I have a problem with the JSON data from the Web API service.
In a regular Web API Controller, I get the result given below.
[
{
"title": "başlık",
"description": "Tanımlama",
"creationTime": "2018-01-15T17:20:06.9801797",
"state": 0,
"assignedPersonId": "afd46520-521d-4945-a4ee-083893e1d14c",
"assignedPersonName": "derya",
"id": 2
},
{
"title": "title",
"description": "description",
"creationTime": "2018-01-15T17:17:26.5161288",
"state": 0,
"assignedPersonId": null,
"assignedPersonName": null,
"id": 1
}
]
But when using ASP.NET Boilerplate infrastructure, I get the same data as:
{
"result": {
"items": [
{
"title": "başlık",
"description": "Tanımlama",
"creationTime": "2018-01-15T17:20:06.9801797",
"state": 0,
"assignedPersonId": "afd46520-521d-4945-a4ee-083893e1d14c",
"assignedPersonName": "derya",
"id": 2
},
{
"title": "title",
"description": "description",
"creationTime": "2018-01-15T17:17:26.5161288",
"state": 0,
"assignedPersonId": null,
"assignedPersonName": null,
"id": 1
}
]
},
"targetUrl": null,
"success": true,
"error": null,
"unAuthorizedRequest": false,
"__abp": true
}
It seems that the actual raw data is nested in an outer data structure. Because of this, deserialization like below doesn't work.
List<Class1> data = JsonConvert.DeserializeObject<List<Class1>>(JSONString);
And I have to manage some string operations on JSONString
.
Am I doing something wrong? Thanks in advance.
From the documentation on WrapResult and DontWrapResult Attributes:
You can control wrapping using WrapResult and DontWrapResult attributes for an action or all actions of a controller.
ASP.NET MVC Controllers
ASP.NET Boilerplate wraps ASP.NET MVC action results by default if return type is JsonResult (or Task<JsonResult> for async actions). You can change this by using WrapResult attribute as shown below:
public class PeopleController : AbpController { [HttpPost] [WrapResult(WrapOnSuccess = false, WrapOnError = false)] public JsonResult SavePerson(SavePersonModel person) { // TODO: save new person to database and return new person's id return Json(new {PersonId = 42}); } }
As a shortcut, we can just use [DontWrapResult] which is identical for this example.
You can change this default behaviour from startup configuration.
This applies not only to ASP.NET MVC Controllers, but also to ASP.NET Web API Controllers, Dynamic Web API Layer and ASP.NET Core Controllers.