11

I'm using Swagger UI with asp.net core web api. For my CRUD operations, I have a base controller class, which defines abstract methods:

public class BaseController<TDto, TEntity>
{
    [HttpPost, Route("create-single")]
    public abstract Task<ObjectResult> CreateAsync(TDto input);

    [HttpPost, Route("create-many")]
    public abstract Task<ObjectResult> CreateManyAsync(IList<TDto> input);

    [HttpGet, Route("get-all")]
    public abstract Task<ObjectResult> GetAllAsync();

    [HttpGet, Route("get/{id}")]
    public abstract Task<ObjectResult> GetByIdAsync(Guid id);

    ...
    ...
}

Some controller might not need all the CRUD methods, and I need to disappear those endpoints from the swagger.

For example, I need to disappear /get/{id} endpoint for particular controller, what is the best way to achieve this?

Arkadi
  • 1,153
  • 1
  • 14
  • 35

1 Answers1

16

You can add the following attribute to Controllers and Actions to exclude them from the generated documentation: [ApiExplorerSettings(IgnoreApi = true)]

romanoza
  • 4,775
  • 3
  • 27
  • 44
Ajay Gupta
  • 703
  • 5
  • 11
  • My controller action produces 4 endpoints (due to 2 optional url arguments). What if I want to keep only the first without the 2 optional arguments and remove the rest? – Thanasis Ioannidis May 18 '23 at 14:40
  • @ThanasisIoannidis you would need a filter for this. Take a look into this: https://stackoverflow.com/a/54788867/2343336 – Rogerson Nazário Jul 11 '23 at 12:23