网络 API 操作

Web API actions

虽然不完全是 RESTful,但我认为在您做的事情比仅仅操作简单数据时在概念上涉及更多的事情时使用操作是有意义的。因此我采用了以下方案:

/api/projects/ = Returns all projects
/api/projects/{id} = Returns single project

/api/projects/{id}/{action} [POST]
       Applies action on a project, such as "Activate"
/api/projects/{action} [GET]
       Gets projects which meet the condition of the action, such
       as all projects which are "Active"

但是...提供一种为客户获取项目的方法不是很清楚,我倾向于选项 2...

1. /api/projects/GetByClient [POST, Client Id in body]
       But now it has to be a POST, when we're only getting a resultset...
2. /api/clients/{clientid}/GetProjects
       But now we're returning projects from a clients controller
3. /api/ProjectGetterForClient/{client}
       But will result in a large number of URLs for a complex project

我应该如何才能使这种 API 可用?

如果#2 是正确的选项。我应该 return 只列出项目列表吗?或者 return 包含项目列表的客户?

我认为最 RESTful 的方式是:

GET: /api/clients/5/projects

这将 return ID 为 5 的客户的所有项目。

您还可以使用:

GET: /api/projects?clientId=5

如果您的来电者在请求客户时可能会使用项目数据,则将其包含在结果中。否则你最好将它们作为单独的请求保存。

您听说过 OData 吗?它扩展了基本的 REST 功能并提供了标准化的查询格式。

例如:

Requesting an Individual Entity by ID

The request below returns an individual entity of type Person by the given UserName “russellwhyte”

GET serviceRoot/People('russellwhyte')

http://www.odata.org/getting-started/basic-tutorial/

或者...

GET serviceRoot/Airports('KSFO')/Location/Address

在你的情况下,我认为或多或少...

GET api/Clients('clientid')/Projects

如 user1620220 所述 - Web API 内置了对 OData 的支持: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options