【发布时间】:2016-09-08 18:30:18
【问题描述】:
目前我在每个控制器中都有以下标准函数来处理基本的 CRUD 操作:
GET /api/todo Get all to-do items
GET /api/todo/{id} Get an item by ID
POST /api/todo Add a new item
PUT /api/todo/{id} Update an existing item
DELETE /api/todo/{id} Delete an item
然而,我意识到我实际上需要传递多个参数来获取在数据库级别过滤的待办事项列表,而不是检索所有项目并使用 linq。
例如,我决定这样做:
在我的控制器中:
// POST: api/todo
[HttpPost]
public IList<TodoItem> Get([FromBody]GetTodoItemsRequest request)
{
return _todoItemManager.GetTodoItems(request.Name, request.CategoryId);
}
如您所见,我创建了一个名为 GetTodoItemsRequest 的新模型,该模型将为我的每个参数提供一个属性。在这种情况下:名称、CategoryId。
我认为在处理多个参数并检索列表时,最好执行 POST 并专门为其创建模型。而不是使用 GET 并在 url 中传递各种信息。
执行上述操作似乎有点奇怪...... msot 会认为这是一个完美的解决方案,还是我在 WebAPI 世界中缺少什么?
【问题讨论】:
标签: c# asp.net asp.net-web-api asp.net-web-api2 asp.net-core