我的 Web Api 2 Controller 上有 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;
}
虽然名称不同,但我得到了错误:
Not supported by Swagger 2.0: Multiple operations with path 'api/Client' and method '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());
但是它被注释掉了,注释掉那行代码,希望这能解决你的问题,尝试一下,因为你可能不只是想要第一个。
在代码行上方,您将看到对其的简短描述。希望这对您有所帮助。
关于asp.net-web-api2 - Swagger 不支持 Swagger 2.0 : Multiple operations with path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39706508/