c# - Swashbuckle - 未显示继承自 BaseController 的 Web API Controller

标签 c# asp.net-web-api swagger swagger-ui swashbuckle

我有一个 OWIN、自托管 Web API 项目,我想向其中添加 Swagger 文档和 Swagger UI。

我在 Startup.cs 中包含了 Swashbuckle.Core 包并手动配置了它。

configuration.EnableSwagger(s =>
{
    s.SingleApiVersion(Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace('.', ' '), "MyApi").;
    s.IncludeXmlComments($@"{System.AppDomain.CurrentDomain.BaseDirectory}\MyApi.XML");
    s.DescribeAllEnumsAsStrings();
})
.EnableSwaggerUi(s =>
{
    // By default, swagger-ui will validate specs against swagger.io's online validator and display the result
    // in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the
    // feature entirely.
    //c.SetValidatorUrl("http://localhost/validator");
    s.DisableValidator();
});

现在,我有一个基础 Controller 和两个继承自基础的附加 Controller 。不幸的是,我在 swagger 页面中看不到 Controller 的名称和操作。

这是我的基本 Controller :

public class BaseController : ApiController
{
    public BaseController()
    {
        // Initialization...
    }

    public async Task<IHttpActionResult> MyAction()
    {
        // Code here...
    }
}

Controller 1:

public class My1Controller : BaseController
{
    public MyController1(...): base(...)
    {
        // Initialization...
    }

    public async Task<IHttpActionResult> Post(Model1 model)
    {
        // Code here...
    }

    public async Task<IHttpActionResult> Post(Model2 model)
    {
        // Code here...
    }
}

Controller 2:

public class My2Controller : BaseController
{
    public My2Controller(...): base(...)
    {
        // Initialization...
    }

    public async Task<IHttpActionResult> Post(Model1 model)
    {
        // Code here...
    }

    public async Task<IHttpActionResult> Post(Model2 model)
    {
        // Code here...
    }
}

我在 swagger 索引页面中看不到 My1ControllerMy2Controller。我在 My1ControllerMy2Controller 上尝试了 ApiExplorerSettings(IgnoreApi = true)] 属性,但没有任何反应。

是否因为 Controller 操作共享通用名称(多个 Post 操作具有不同的参数类型)?我并没有像上面的示例那样使用 RPC 样式的 URL,而是遵循 5 级媒体类型 (5LMT) 提案的 RESTful URL。

有什么建议吗?

最佳答案

ASP.NET Web API 适用于 naming conventions ,这意味着您的 Controller 未被解析,因为它们的名称没有以 Controller 结尾。

这意味着 MyController1 变成了 My1Controller

例如,将 MyController1 更改为以下内容即可:

public class My1Controller : BaseController
{
    public MyController1(...): base(...)
    {
        // Initialization...
    }

    public async Task<IHttpActionResult> Post(Model1 model)
    {
        // Code here...
    }

    public async Task<IHttpActionResult> Post(Model2 model)
    {
        // Code here...
    }
}

关于c# - Swashbuckle - 未显示继承自 BaseController 的 Web API Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39230269/

相关文章:

c# - 如何使用 Mongodb/Couchdb 创建我的 DDD 实体?

c# - 如何使用 EF 迁移将 int ID 列更改为 Guid?

C# 开关语法错误

c# - 将 OData 属性配置为 Date 而不是 DateTime 类型

asp.net-web-api - IFormFile 的 Asp.Net Core swagger 帮助页面

Swagger/Swashbuckle : OAuth2 with Resource Owner Password Credentials Grant

java - Micronaut(Gradle 和 Java)-启用安全性后无法访问 Swagger 集成 View

c# - 不确定何时在 C# 中使用 'base'

c# - WebAPI PushStreamContent 远程主机关闭连接

java - Swagger Codegen 将 "format: time"转换为非字符串