isaac.hazan
isaac.hazan

Reputation: 3874

How to remove the Controller Name from Swagger Documentation methods

I am using Swashbuckle for having the Swagger documentation in my C# controller If I have a controller as follows:

public class CommandController : ApiController
{
    public CommandResult Cancel(...)
    { ...}

    public CommandResult Status(...)
    { ...}

}

Then I can get the doc through http://localhost/swagger/ui/docs and I generate the doc in HTML or any other format, it will look as follows:

REST API Docs

In the above the commands on left side are are prefixed with the name of the controller.

Is there a way to remove the controller prefix and have only the name of the method?

Upvotes: 1

Views: 2250

Answers (4)

Charles Burns
Charles Burns

Reputation: 10602

Simply add the ApiExplorerSettings(IgnoreApi = true) attribute.

Example:

namespace Example.Controllers;    
[ApiExplorerSettings(IgnoreApi = true), Route("[controller]")]
public class ExampleController : ControllerBase
{
    [HttpDelete, Route("UndocumentedDeleteMethod")]
    public IActionResult UndocumentedDeleteMethod(int someId)
    { ... }
}

This assumes use of Swashbuckle.

Upvotes: 2

Andrew
Andrew

Reputation: 1526

For anyone encountering issues with the swagger generated interfaces, models, or service names, you may find this bash script helpful, which pre-processes the swagger file, and post-processes the generated output files.

Upvotes: 0

mortifactor
mortifactor

Reputation: 21

I had similar question and I just found it in official doc (link)

Option 2) Provide a custom strategy

// Startup.cs
services.AddSwaggerGen(c =>
{
    ...

    // Use method name as operationId
    c.CustomOperationIds(apiDesc =>
    {
        return apiDesc.TryGetMethodInfo(out MethodInfo methodInfo) ? methodInfo.Name : null;
    });
})

// ProductsController.cs
[HttpGet("{id}")]
public IActionResult GetProductById(int id) // operationId = "GetProductById"

Upvotes: 2

Bryan Lewis
Bryan Lewis

Reputation: 5978

The response (by NanoWar) to this GitHub issue seems to answer your question: https://github.com/swagger-api/swagger-codegen/issues/6164

It involves adding a OperationFilter to your SwaggerConfig.

Upvotes: 1

Related Questions