Swagger不列出C#默认接口实现
要在我的C#ASP.NET Core Controller上使用构图 - 跨性别,我在控制器中添加了几个接口。这些接口包含“默认接口实现”(C#8.0中的新接口实现),因此我可以将这些接口添加到我的几个控制器中,以允许重新使用这些端点。但是,Swagger不会列出这些方法。
示例代码:
public class MyController : IMyReusableReadInterface<MyViewModel>
{
public async Task<ActionResult<MyViewModel>> MyDomainAction(...)
{
// Implementation goes here
}
}
public interface IMyReusableReadInterface<IViewModel>
{
public async Task<ActionResult<IViewModel>> Get<IViewModel>(...)
{
// Implementation goes here
}
}
在此示例中,Action myDomainAction
在Swagger上列出,但是GET
方法未列出接口。如何检测到ASP.NET核心控制器上的方法?知道为什么未检测到默认接口实现吗?还是我做错了什么?
编辑: @Davidg问为什么不使用继承:因为我想组合我的控制器,而不会被迫使用所有继承的基本控制器。例如,我现在可以添加iwritecontrollerbase
而无需添加ireadcontrollerBase
(是的,是人为的例子,请携带我)。如果我使用继承,writecontrollerbase
将扩展read controllerBase
,也将获得那些不需要的方法。
To use composition-over-inheritance on my C# ASP.NET Core controllers, I added several interfaces to my controllers. These interfaces contain 'default interface implementations' (new in C# 8.0), so I can add those interfaces to several of my controllers to allow re-use of those endpoints. However, Swagger will not list those methods.
Example code:
public class MyController : IMyReusableReadInterface<MyViewModel>
{
public async Task<ActionResult<MyViewModel>> MyDomainAction(...)
{
// Implementation goes here
}
}
public interface IMyReusableReadInterface<IViewModel>
{
public async Task<ActionResult<IViewModel>> Get<IViewModel>(...)
{
// Implementation goes here
}
}
In this example, the action MyDomainAction
is listed on Swagger, but the Get
method from the interface is not listed. How are methods on an ASP.NET Core controller detected? Any idea why default interface implementations are not detected? Or am I doing something wrong?
EDIT:
@DavidG asked why not use inheritance: Because I want to compose my controller, without being forced to use all inherited base controllers. For example, I can now add IWriteControllerBase
without adding IReadControllerBase
(yes, contrived example, bear with me). If I would use inheritance, WriteControllerBase
would extend ReadControllerBase
, and would also get those unwanted methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论