asp.net-web-api2 - Swagger 不支持 Swagger 2.0 : Multiple operations with path

标签 asp.net-web-api2 swagger

我的 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/

相关文章:

c# - 如何防止创建处理同一请求的多个后台进程?

c# - ASP.NET WebAPI 2 OData Controller 中的自定义 GET 操作

c# - 如何使用 web api 获取资源的可用性?

asp.net - 缺少 Web Api 的 swagger REST 描述

asp.net - DotNet 5.0 Swagger 未在 Azure 应用服务中加载

swagger - 请求正文未在 Nest.js + Swagger 中显示

c# - 如何让 WebAPI 使用 JsonProperty(Required = Required.Always) 验证我的 JSON?

java - 无法 Swagger 工作(wildfly 9)

c# - 使用 JWT(授权 : Bearer) in Swagger in ASP. NET Core

javascript - 限制文本输入,以便在 html 页面中使用 Web API 时排除 html 或脚本