C# RESTAPI中路由关系的最佳实践是什么?

C# RESTAPI中路由关系的最佳实践是什么?,c#,asp.net-web-api,routing,attributerouting,C#,Asp.net Web Api,Routing,Attributerouting,假设我有两个对象,用户和约会,其中用户可能有一个或多个约会 我很好奇,我是否应该允许API的使用者通过约会控制器的用户进行“过滤”。下面是我对GET操作的一些调用: Users Controller GET api/Users api/Users/{userId} api/Users/{userId}/Appointments api/Users/{userId}/Appointments/{appointmentId} Appointments Controller GET api/Appo

假设我有两个对象,用户和约会,其中用户可能有一个或多个约会

我很好奇,我是否应该允许API的使用者通过约会控制器的用户进行“过滤”。下面是我对GET操作的一些调用:

Users Controller
GET
api/Users
api/Users/{userId}
api/Users/{userId}/Appointments
api/Users/{userId}/Appointments/{appointmentId}

Appointments Controller
GET
api/Appointments/
api/Appointments/{appointmentId}
我很好奇为约会控制器上的用户实现过滤器是否是一个好主意(良好实践)。。。因此,上述呼吁将变成:

Appointments Controller
GET
api/Appointments/{userId:int?}
api/Appointments/{appointmentId}
在上面的调用中,我为userId添加了一个可选的querystring参数,该参数默认为null。这将允许调用者获取所有约会,或者通过userId获取约会


我想我真的不确定什么是关系对象的最佳实践。

如果您想要简单,我建议您

使用者 得到

任命 得到

从它的外观来看,
api/appoints/{userId:int?}
读起来并不好,因为用户有约会,而约会只有一个用户。在端点
api/Appointments/{appointmentId}
的情况下,我不确定
appointmentId
是什么,但是如果它是int的话,你必须做一些有趣的事情来根据int查找约会或用户,在这种情况下,ID可能是相同的


深入到对象树的深度太深可能会让url结构变得混乱。

如果您想要简单,我建议您

使用者 得到

任命 得到

从它的外观来看,
api/appoints/{userId:int?}
读起来并不好,因为用户有约会,而约会只有一个用户。在端点
api/Appointments/{appointmentId}
的情况下,我不确定
appointmentId
是什么,但是如果它是int的话,你必须做一些有趣的事情来根据int查找约会或用户,在这种情况下,ID可能是相同的

深入到对象树的深度太深可能会导致url结构混乱

api/users
api/user/{id}
api/user/{id}/appointments
api/appointments
api/appointment/{id}