简体   繁体   English

REST API的GET和POST / PUT资源不同吗?

[英]REST API different resources for GET and POST/PUT?

I'm currently in the process of designing as RESTful of an API as I can using Microsoft's Web API 2 in C#. 我目前正在设计API的RESTful,可以在C#中使用Microsoft的Web API 2。 What I'm struggling on is how best to represent resources or the proper way to do it where the GET call and POST/PUT are very different. 我正在努力的是如何最好地表示资源或在GET调用和POST / PUT有很大不同的情况下正确地使用资源。

For example say I have something calls states that have an id, name, status, etc., these can be assigned to a document. 例如,说我有一些呼叫状态,这些状态具有ID,名称,状态等,这些状态可以分配给文档。 So I have a route like this /documents/{id}/states/ . 所以我有一条这样的路由/ documents / {id} / states /。 If I call a GET here I need to get the full list of all assigned states including their id, name, etc. 如果我在此处调用GET,则需要获取所有已分配状态的完整列表,包括其ID,名称等。

However, in order to change which states are assigned to the document I simply need to pass the id. 但是,为了更改分配给文档的状态,我只需要传递id。 I cannot do this individually, it must be an array that gets sent up since users may be interacting with hundreds or thousands at a time. 我不能单独执行此操作,它必须是发送出去的数组,因为用户一次可能与成百上千的用户交互。

So in this case I have a few issues. 所以在这种情况下,我有几个问题。 I don't even know if POST or PUT is correct here, and second whichever one it is can I just take in an array of integers? 我什至不知道POST或PUT在这里是否正确,第二个可以输入整数数组吗?

In your case, I would suggest PUT is the method you would be wanting to use, as you know the location of the resource that you are updating. 对于您的情况,我建议您使用PUT作为您要使用的方法,因为您知道要更新的资源的位置。 For more info, see here: http://restcookbook.com/HTTP%20Methods/put-vs-post/ 有关更多信息,请参见此处: http : //restcookbook.com/HTTP%20Methods/put-vs-post/

In ASP.NET Web API 2 you can use the [FromBody] parameter attribute, so that your method signature would be: public void UpdateStates(int id, [FromBody]List<int> states) {} More info on parameter attributes can be found here: http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api 在ASP.NET Web API 2中,可以使用[FromBody]参数属性,以便方法签名为: public void UpdateStates(int id, [FromBody]List<int> states) {}有关参数属性的更多信息public void UpdateStates(int id, [FromBody]List<int> states) {}在此处找到: http : //www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM