我有一个这样的 Controller
[Route("api/Foo/MyController/{id}")]
public IHttpActionResult Get(int id)
{
//Code code
foo(id); // Foo accept Int or Null
}
这确实有效,如果调用 api/Foo/MyController/1,但我需要调用 api/Foo/MyController 就像它“GetAll”参数 id 现在是null 和 stuff into controller 返回所有,怎么去那里?
最佳答案
你可以添加一个新的方法和路由:
[Route("api/Foo/MyController")]
public IHttpActionResult Get()
{
//Code code
}
编辑:要重用您可以使用可选参数的相同方法:
[Route("api/Foo/MyController/{id}")]
public IHttpActionResult Get(int? id)
{
if (id.HasValue)
{
// get by id
}
else
{
// get all
}
}
关于c# - WebAPI Controller GetAll 或 Get some Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45314108/