方法未在 Swagger UI(带 Swashbuckle)中显示,但没有错误消息

Methods don't show in Swagger UI (w/ Swashbuckle) but no error message

我正在用 Swashbuckle 编写 api,但 运行 遇到了问题。我的 AController 中有三个 Get 方法:

[ResponseType(typeof(IEnumerable<A>))]
public IHttpActionResult Get(int bId);

[ResponseType(typeof(IEnumerable<A>))]
public IHttpActionResult Get(IList<int> cIds);

[ResponseType(typeof(IEnumerable<A>))]
public IHttpActionResult Get(int bId, IList<int> cIds);

我希望出现关于重载端点的错误,或者它可以使用具有两个可选参数的单个 get 方法,涵盖所有情况。相反,我只显示了第二种方法,这似乎是任意的。

我想知道,是否有任何方法可以使这三个方法都起作用,而且,UI 中显示第二种方法是否有偶然的原因?谢谢!

要回答您的第一个问题,您可以通过为它们定义不同的端点来使所有三种 GET 方法工作。对于每个 GET 方法,添加以下属性:[HttpGet("the_end_point_you_want")].

即)

[HttpGet("/GetbID/{bId})")
[ResponseType(typeof(IEnumerable<A>))]
public IHttpActionResult Get(int bId)

[HttpGet("/GetListcId")]
[ResponseType(typeof(IEnumerable<A>))]
public IHttpActionResult Get(IList<int> cIds)

对于你的第二个问题,我不确定为什么选择第二个 GET,但我的猜测是 SwashBuckle 如何检索每个 GET 方法。

希望对您有所帮助