Roles
角色看起来是一个非常明智的子资源。
假设您想要使用 http 动词列出和删除用户的角色...那么您可以使用两个控制器,一个用于用户,另一个用于角色。您的路线将如下所示:
config.Routes.MapHttpRoute(
name: "UserApi",
routeTemplate: "api/v1/Users/{userId}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
然后,您的角色控制器将具有以下方法:
public class RolesController : ApiController
{
// GET api/v1/users/1/roles
public IEnumerable<Role> Get(int userId)
{
//
}
// GET api/v1/users/1/roles/1
public IEnumerable<Role> Get(int userId, int id)
{
//
}
}
角色作为用户的部分响应
对于请求使用什么格式和标准:
apigee 在其网站上制作了一本免费电子书,其中提出了设计建议以及对现有 API 的观察。
他们描述了来自 LinkedIn、Facebook 和 Google 的部分响应示例。他们的一篇博客对此进行了介绍here http://blog.apigee.com/detail/restful_api_design_can_your_api_give_developers_just_the_information在他们的书中here http://blog.apigee.com/detail/announcement_new_ebook_on_web_api_design.
如何使用 WebApi ASP.NET
假设使用 JSON 作为内容类型,那么之前已经问过类似的问题ASP.NET Web API部分响应Json序列化 https://stackoverflow.com/questions/13606675/asp-net-web-api-partial-response-json-serialization,简而言之,您需要将“?Fields=”或类似的查询参数传递到自定义中ContractResolver
.