【问题标题】:Swagger Not supported by Swagger 2.0: Multiple operations with pathSwagger 2.0 不支持 Swagger:带路径的多个操作
【发布时间】: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 }
    );

【问题讨论】:

    标签: asp.net-web-api2 swagger


    【解决方案1】:

    我希望这对你有帮助,我用这种方式解决了我的问题,转到你的 swagger 配置文件,在顶部添加以下内容:

    using System.Linq;
    

    然后在第 175 行附近,或者使用搜索并找到 ResolveConflictingActions 你应该找到这行代码:

    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
    

    但是它被注释掉了,注释那行代码,希望这能解决你的问题,玩弄它,因为你可能不只是想要第一个。

    在代码行上方,您会看到它的简短描述。希望这对您有所帮助。

    【讨论】:

    • 很高兴能帮到你:)
    猜你喜欢
    • 2017-01-28
    • 2019-10-16
    • 2017-05-19
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多