20

I used NSwag to generate a client for a single controller; I needed it as its own separate client. I would like for it to be ignored when the Swagger specification is generated in the future.

I tried adding this attribute at the top of the controller but it is still being noted in the specification: [ApiExplorerSettings(IgnoreApi = true)]

My controller is implementing the standard Microsoft.AspNetCore.Mvc.Controller class.

James B. Nall
  • 1,398
  • 3
  • 16
  • 27

3 Answers3

43

I think in the latest version,

[ApiExplorerSettings(IgnoreApi = true)]

is supported.

Otherwise you can add the SwaggerIgnoreAttribute OR OpenApiIgnoreAttribute attribute

[SwaggerIgnore]
[OpenApiIgnore]

Or manually select the controllers in NSwagStudio or in the middleware...

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
  • 4
    I have the latest version but that annotation still didn't work for me. Using `[NSwag.Annotations.SwaggerIgnore]` ended up being the best solution. – James B. Nall Apr 18 '17 at 21:18
  • Strange: https://github.com/NSwag/NSwag/blob/master/src/NSwag.SwaggerGeneration.WebApi.Tests/Attributes/ApiExplorerSettingsAttributeTests.cs – Rico Suter Apr 18 '17 at 21:50
  • That test only demonstrates that attribute works when placed over a method. I was interested in knocking out the entire controller with one attribute. `[SwaggerIgnore]` does just that, thankfully. – James B. Nall Apr 19 '17 at 14:39
  • 1
    I see, next version will also support this scenario: https://github.com/NSwag/NSwag/commit/bbebc00af5f4c490049db9e77f440c7693069f38 – Rico Suter Apr 19 '17 at 17:39
  • 2
    `[Microsoft.AspNetCore.Mvc.ApiExplorerSettings(IgnoreApi = true)]` is needed if you also have `using System.Web.Http.Description;` in your Controller – Saibamen Feb 03 '22 at 08:32
17

Use [OpenApiIgnore]

(since [SwaggerIgnore] has been deprecated)

Arieh
  • 667
  • 8
  • 11
0

@Arieh 's answer worked for me. I'd like to share more.

  1. Install package NSwag.Annotations for DTO class, for my case which is in separated project, then use [OpenApiIgnore] decorate the property which should be ignored.
  2. Install package NSwag.AspNetCore for the web project.
xingzheng
  • 1
  • 1