【发布时间】:2016-09-26 15:16:31
【问题描述】:
我的 Web Api 2 控制器上有 2 个 Get 方法:
// GET: api/ClientApi
public HttpResponseMessage GetAll()
{
IEnumerable<Client> clients = _clientRepository.GetAll();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, clients);
return response;
}
// GET: api/ClientApi/userId
public HttpResponseMessage GetAllClientsForUser(string userId)
{
IEnumerable<Client> clients = _clientRepository.GetAll();
var clientUsers = _clientUserRepository.FindAll(cu => cu.UserId == userId).ToList();
var clientsForUser = clients.Where(i => clientUsers.Select(cu => cu.ClientId).ToList().Contains(i.Id)).ToList();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, clientsForUser);
return response;
}
// GET: api/ClientApi/clientId
public HttpResponseMessage GetClientById(int id)
{
var client = _clientRepository.GetById<Client>(id);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, client);
return response;
}
虽然名称不同,但我得到了错误:
Swagger 2.0 不支持:带路径的多个操作 'api/Client' 和方法 'GET'。
有没有办法解决这个问题?我尝试使用 OperationFilter,在某个地方的 StackOverflow 上找到了这个,但这不起作用...
Route.config:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
【问题讨论】: