将子数据协定作为Rest URL(REST API)返回的最佳实践

时间:2023-01-05 21:17:50

I'm working on an API using WCF-RestAPI. I'm hitting a problem with our GETs are returning too much information contained in child entities (data contracts). We have decided to instead, return a URL which should be accessed to get the child entity.

我正在使用WCF-RestAPI开发API。我遇到了一个问题,我们的GET正在返回子实体中包含的太多信息(数据合同)。我们决定返回一个URL,该URL应该被访问以获取子实体。

So for example;

所以例如;

{
  "date": "2014-12-01T00:00:00Z",
  "contractor": {
    "contractorReference": "DEFREF",
    "contractorName": "Default Supplier",
    "mainTelephone": "123456789",
    "mainAddress": {
      "fullAddress": "Default Supplier Street DefaultTown United Kingdom"
    },
    "mainFax": null,
    "webAddress": null,
    "comment": null
  },
  "moreinfo": "data"
}

would become something like

会变得像

{
  "date": "2014-12-01T00:00:00Z",
  "contractor": "https://rest-api/contractor/{id}",
  "moreinfo": "data"
}

Is there anything built into REST API or a standard way of doing this? I'm considering creating an attribute on the data contract possibly named "IsLinkable" and picking this up on an action filter on serialization. Not sure this is the best solution though.

REST API中是否有任何内置或标准方法?我正在考虑在数据契约上创建一个名为“IsLinkable”的属性,并在序列化的动作过滤器上选择它。不确定这是最好的解决方案。

1 个解决方案

#1


0  

I didnt find anything built into the Framework that would do this out of box.. I got round the issue by replacing the child data contracts with string properties for holding the URLs. These would then be built up using reflection and attributes on the data contract themselves.

我没有找到框架内置的任何内容可以解决这个问题。我通过用字符串属性替换子数据协定来解决问题。然后,这些将使用数据协定本身的反射和属性构建。

i.e. I had a property holding the child entity's unique identifier (e.g. UserId) which I specified in the attribute along with the route.. (e.g. Users/{id}) . I then set up an action filter which would execute just before returning the populated data contract to the user, this would build the URLs using the method mentioned above.

即,我有一个属性,其中包含我在属性中指定的子实体的唯一标识符(例如UserId)以及路径..(例如Users / {id})。然后,我设置了一个动作过滤器,它将在将填充的数据合同返回给用户之前执行,这将使用上述方法构建URL。

#1


0  

I didnt find anything built into the Framework that would do this out of box.. I got round the issue by replacing the child data contracts with string properties for holding the URLs. These would then be built up using reflection and attributes on the data contract themselves.

我没有找到框架内置的任何内容可以解决这个问题。我通过用字符串属性替换子数据协定来解决问题。然后,这些将使用数据协定本身的反射和属性构建。

i.e. I had a property holding the child entity's unique identifier (e.g. UserId) which I specified in the attribute along with the route.. (e.g. Users/{id}) . I then set up an action filter which would execute just before returning the populated data contract to the user, this would build the URLs using the method mentioned above.

即,我有一个属性,其中包含我在属性中指定的子实体的唯一标识符(例如UserId)以及路径..(例如Users / {id})。然后,我设置了一个动作过滤器,它将在将填充的数据合同返回给用户之前执行,这将使用上述方法构建URL。