[英]Swagger Not supported by Swagger 2.0: Multiple operations with path
我的 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 上的某个地方找到了它,但这不起作用......
路由配置:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我希望这对您有所帮助,我以这种方式解决了我的问题,转到您的 swagger 配置文件,在顶部添加以下内容:
using System.Linq;
然后在第 175 行左右,或者使用搜索并找到 ResolveConflictingActions,您应该找到这行代码:
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
但它被注释掉了,注释掉那行代码,希望这能解决你的问题,试试看,因为你可能不只是想要第一个。
在代码行上方,您将看到它的简短描述。 希望这是有帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.