简体   繁体   English

WebAPI2中正确的RESTful实现是什么?

[英]What is the correct RESTful implementation in WebAPI2?

Essentially I have found a waste number of opinions on this topic, but none that convinces me what is correct. 基本上我发现了关于这个主题的废话,但没有一个能说服我什么是正确的。 More specifically I have the following questions: 更具体地说,我有以下问题:

Singular or Plural or both 单数或复数或两者兼而有之

  • Is there even a correct and a wrong way? 甚至有正确和错误的方式吗?

    Standards and conventions exists for a reason, I don´t believe I will ever settle for - it´s just a matter of taste . 标准和惯例存在是有原因的,我不相信我会满足 - 这只是一个品味问题 Isn´t there any guideline or standard? 没有任何指南或标准吗? No authority completing Fieldings work? 没有权力完成Fieldings工作?

What IHttpActionResults to return? IHttpActionResults返回什么? And what should they contain? 它们应该包含什么?

  • Get - return Ok(); 获取 - 返回Ok();
  • Post - return Created(); Post - return Created();
  • Put - return (Put? Ok?) Put - return(Put?Ok?)
  • Delete - return (Deleted? Ok?) 删除 - 返回(删除?好吗?)

What to return in Created( location )? 在Created( 位置 )返回什么?

Assuming the Controller route is 'api/v1/model', should it be 假设Controller路由是'api / v1 / model',它应该是

I ask these questions because I frequently run into conflicts about how to best implement APIs. 我问这些问题是因为我经常遇到如何最好地实现API的冲突。

Q: Singular or Plural or both 问: 单数或复数或两者兼而有之

Never use both. 绝不使用两者。 Use either one. 使用任何一个。 Use noun but not verbs. 使用名词而不是动词。

Do not use verbs: 不要使用动词:

/getAllCars / getAllCars
/createNewCar / createNewCar
/deleteAllRedCars / deleteAllRedCars

Do not mix up singular and plural nouns. 不要混淆单数和复数名词。 Keep it simple and use only plural nouns for all resources. 保持简单,只使用复数名词来表示所有资源。

/cars instead of /car
/users instead of /user
/products instead of /product

Now if you see below, it will make more sense: 现在,如果您在下面看到,它将更有意义:

GET /tickets - Retrieves a list of tickets GET / ticket - 检索故障单列表
GET /tickets/12 - Retrieves a specific ticket GET / tickets / 12 - 检索特定故障单
POST /tickets - Creates a new ticket POST / tickets - 创建新票证
PUT /tickets/12 - Updates ticket #12 PUT / tickets / 12 - 更新门票#12
PATCH /tickets/12 - Partially updates ticket #12 PATCH / tickets / 12 - 部分更新机票#12
DELETE /tickets/12 - Deletes ticket #12 DELETE / tickets / 12 - 删除12号门票

If a resource is related to another resource use subresources. 如果资源与另一个资源相关,则使用子资源。

GET /cars/711/drivers/ Returns a list of drivers for car 711 GET / cars / 711 / drivers /返回汽车711的驱动程序列表

Q: What to return in Created(location)? 问: 在Created(位置)返回什么?

200 OK - Response to a successful GET, PUT, PATCH or DELETE. 200 OK - 响应成功的GET,PUT,PATCH或DELETE。 Can also be used for a POST that doesn't result in a creation. 也可以用于不会导致创建的POST。

201 Created - Response to a POST that results in a creation. 201 Created - 对POST的响应,导致创建。 Should be combined with a Location header pointing to the location of the new resource 应与指向新资源位置的Location标头结合使用

Provide clarity on your last question and I'll update my answer accordingly. 提供上一个问题的清晰度,我会相应地更新我的答案。

There're no defined standards for REST and everyone uses best practices as per their need. REST没有明确的标准,每个人都根据自己的需要使用最佳实践。 However I would suggest you to go through this PDF from apigee.com which lists down the best practices for REST API and what each of the big player ie Facebook, twitter etc uses. 但是我建议你浏览apigee.com上的这篇PDF文章,其中列出了REST API的最佳实践以及每个大型播放器即Facebook,Twitter等使用的内容。

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

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