如何根据某些表以一种宁静的方式决定资源

时间:2012-08-27 11:39:30

标签: api rest asp.net-mvc-4 asp.net-web-api

我需要知道如何确定什么是资源以及什么是子资源。我只是有一些桌子,我需要制作uri的。所以,假设我有一个学生表和学生类(给出了学生注册的课程的名称和可能的更多细节)表(N:1)。

我将使学生成为主要资源并设计如下所示

http://home/someapi/v1/student/1/

遵循建立uri的休息方法

Qus 1:我必须在访问学生班级时使用以下所示的内容

1.http://home/someapi/v1/studentclass?studentid=1 or
2.http://home/someapi/v1/student/1/studentclass

我是否必须将studentclass视为单独的资源..?

Qus 2:假设表之间存在N:N关系那么uri的设计应该如何?

2 个答案:

答案 0 :(得分:2)

我建议您处理两个资源:studentclass

您可以使用以下URI获取student

http://home/someapi/v1/students/1

请注意这里的复数,如果您想了解为什么要使用复数,请阅读本文:http://blog.apigee.com/detail/restful_api_design_plural_nouns_and_concrete_names/

如果可以在一个student中注册class,则可以成为该学生的属性,因此您可以返回以下JSON文档(例如):

{
  "student": {
    "id": 1,
    "firstName": "John",
    "lastName": "Doe",
    "class": {
      "id": 10,
      "name": "Room Y"
    }
  }
}

您可以使用以下URI获取特定的class

http://home/someapi/v1/classes/10

您可以使用此URI获取给定班级的所有学生:

http://home/someapi/v1/classes/10/students

现在,如果studentsclasses之间存在 N-N 关系,则会改变我关于JSON中student表示的第一个提案。实际上,class关系将成为class资源的集合,而不是student的嵌入属性,您应该使用以下URI:

http://home/someapi/v1/students/1/classes

含义“检索学生#1的所有课程。”

您可以公开classstudent资源。因为他们描述了两个不同的东西,所以不需要决定哪一个是主要的。通过/students/classes,您可以创建studentsclasses,您可以将它们链接起来等。

最后,建议使用查询参数(?foo=bar)进行过滤。

答案 1 :(得分:0)

我认为StudentClass(或简称“Class”)应该是一个不同的资源,因为Class可以在学生之间共享。所以这个Uri会满足你的需求:

http:home / someapi / v1 / studentclass?studentid = 1