Search code examples
c#asp.net-coreswagger

How to exclude certain action from Swagger?


I have a BaseController that defines an action method this way:

    [HttpGet("Duplicate/{dup}")]
    public async Task<IActionResult> DuplicateAsync(string dup)
    {
        if (DuplicateIdRegEx().IsMatch(dup))
        {
            if (int.TryParse(dup.AsSpan(1), out int id))
                return await EditAsync(id, true);
        }

        return NotFound();
    }

That BaseController is the base class of all other controllers.

When I run Swagger, it parses every route, and since the Duplicate action is repeteated for all of the inherited controllers, Swagger is not run, throwing an exception.

How can I avoid Swagger parser to parse that action route?


Solution

  • Try decorating your action with [ApiExplorerSettings(IgnoreApi = true)]

    [ApiExplorerSettings(IgnoreApi = true)]
    [HttpGet("Duplicate/{dup}")]
    public async Task<IActionResult> DuplicateAsync(string dup)
    {
     ...
    

    Doc Official