Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle - Web API Controllers inheriting from BaseController are not shown

I have an OWIN, self-hosted Web API project and I want to add Swagger documentation and Swagger UI to it.

I have included the Swashbuckle.Core package and I have configured it manually, in Startup.cs.

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();
});

Now, I have a base controller and two additional controllers that inherit from the base. Unfortunately I don't see the controllers' names and actions in swagger page.

Here is my base controller:

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

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

Controler1:

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...
    }
}

Controller2:

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...
    }
}

I can't see the My1Controller nor the My2Controller in swagger index page. I have tried the ApiExplorerSettings(IgnoreApi = true)] attribute on My1Controller and My2Controller but nothing happened.

Is it because controllers actions share common names (multiple Post actions with different parameter types)? I am not using RPC-style URLs as it seems from the example above, rather RESTful URLs, following the 5 Levels of Media Types (5LMT) proposal.

Any suggestion?

like image 826
gdyrrahitis Avatar asked Oct 29 '22 21:10

gdyrrahitis


1 Answers

ASP.NET Web API works with naming conventions, which means that your controllers are not parsed because they don't have their names ending with Controller.

This means MyController1 becomes My1Controller.

So as an example, changing MyController1 to the following will work:

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...
    }
}
like image 136
Benjamin Soulier Avatar answered Nov 15 '22 05:11

Benjamin Soulier