无法使用JsonApiFramework

时间:2016-12-19 19:31:35

标签: c# .net json-api jsonapiframework

使用https://github.com/scott-mcdonald/JsonApiFramework框架

我从我的用户界面发送以下PATCH:

{"data":{"id":"4221fe2f-ca7b-425d-b45a-a09f4936a22b","attributes":{"contact-trade-name":"asdad","contact-name":"aasd","contact-phone":"asdad","contact-email":"asdad@adasd.asd","created-date":"2016-12-19T14:07:59.833","status":"AwaitingBillData"},"relationships":{"share-user":{"data":{"type":"users","id":"5078b2ee-2348-44f5-95a0-3c14c2ad2b1d"}}},"type":"jobs"}}

在我的服务器上,我正在执行以下操作:

using (var documentContext = new DocumentContext(HttpContext.Current.Request.Url, document)) { var job = documentContext.GetResource<Job>(); var relationships = documentContext.GetResourceRelationships(job); var shareUserRel = relationships["share-user"]; var shareUser = documentContext.GetRelatedResource<Data.ServiceModel.User>(shareUserRel); }

但是shareUser总是null

当我检查它时,

shareUserRel有以下内容:

{ResourceIdentifier [type=users id=5078b2ee-2348-44f5-95a0-3c14c2ad2b1d]}

如果有帮助的话。

有什么明显可能我做错了吗?

1 个答案:

答案 0 :(得分:0)

要格式化json:api文档以提高可读性,它看起来像这样:

{
  "data": {
    "type": "jobs"
    "id": "4221fe2f-ca7b-425d-b45a-a09f4936a22b",
    "attributes": {
      "contact-trade-name": "asdad",
      "contact-name": "aasd",
      "contact-phone": "asdad",
      "contact-email": "asdad@adasd.asd",
      "created-date": "2016-12-19T14:07:59.833",
      "status": "AwaitingBillData"
    },
    "relationships": {
      "share-user": {
        "data": {
          "type": "users",
          "id": "5078b2ee-2348-44f5-95a0-3c14c2ad2b1d"
        }
      }
    }
  }
}

GetRelatedResource返回null的原因是您要求文档上下文返回文档中不存在的文档中实际包含的用户资源。

当客户端在PATCH中发送此类型的json:api文档时,您指示服务器使用给定属性更新作业资源,并将此作业资源关联到在一对一资源中指定的现有用户资源键。

在您的示例中拥有关系对象后,更正确的内容如下:

if (shareUserRel.IsResourceLinkageNullOrEmpty())
{
  // Clear any linkage between the job resource and any current user
  ...
}
else
{
  var shareUserResourceIdentifier = shareUserRel.GetToOneResourceLinkage();
  // Create linkage between the job resource and existing user identified
  // by shareUserResourceIdentifier
  ...
}

现在你如何处理'...'取决于你的数据层。