Umbraco REST API在嵌套内容

时间:2018-01-16 08:52:58

标签: c# json rest umbraco

如果此项包含Umbraco.NestedContent属性,则无法通过REST API的PublishedContentController获取内容项。 即使嵌套内容类型没有属性或具有任何数量的任何类型属性。如果属性为null或空数组,则REST API工作的唯一方法。

我使用空的Asp.NET Web应用程序,UmbracoCms 7.7.8和UmbracoCms.RestApi 0.9.0。

响应中的例外:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": null,
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Self referencing loop detected for property 'nestedProperty' with type 'System.Collections.Generic.List`1[Umbraco.Core.Models.IPublishedContent]'. Path 'properties.nestedProperty[0].contentSet[0]'.",
        "ExceptionType": "Newtonsoft.Json.JsonSerializationException",
        "StackTrace": "   в Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   ..."
    }
}

但是当我通过ContentController请求相同的数据时,没有错误,并且嵌套属性使用json序列化数据正确返回。示例如下:

{
    "templateId": 0,
    "hasChildren": false,
    "sortOrder": 0,
    "contentTypeAlias": "hostType",
    "parentId": -1,
    "writerId": 0,
    "creatorId": 0,
    "path": "-1,1063",
    "level": 1,
    "id": 1063,
    "key": "cc0558a3-19d9-4970-811e-9fb722016b6d",
    "name": "host1",
    "createDate": "2018-01-16T10:35:29.4+00:00",
    "updateDate": "2018-01-16T11:24:29.887+00:00",
    "itemType": 0,
    "properties": {
        "nestedProperty": "[{\"key\":\"c78e66df-236f-421e-ab83-769bb7e1282d\",\"name\":\"Item 1\",\"ncContentTypeAlias\":\"nestedType\"},{\"key\":\"6f92ca74-ed5e-4565-9e9b-f7a9dfe574b1\",\"name\":\"Item 2\",\"ncContentTypeAlias\":\"nestedType\"},{\"key\":\"f58d0d7a-f98d-4a2c-87da-13ac705aa679\",\"name\":\"Item 3\",\"ncContentTypeAlias\":\"nestedType\"}]"
    },
    "_links": {
        ...
    }
}

我在空的SqlCe数据库项目上测试过它。

有谁知道为什么会发生这种情况以及如何使PublishedContentController工作?

0 个答案:

没有答案