13

EDIT: I'm aware of what attributes in general do, the question is for this specific attribute alone. Sorry for the confusion!

I've read the following question, along with this one, which point to how the attribute is used to ignore the generated swagger/swashbuckle documentation for specific methods or whole controllers. (documentation is the swagger page with all the api's listed I believe?)

But other than swagger/swashbuckle (which is a NuGet package), what other function does this attribute possess in ASP.NET?

SpiritBob
  • 2,355
  • 3
  • 24
  • 62
  • It's unclear whether you don't know what attributes are at all or whether you understand attributes but don't understand what this specific one does. The two possible answers are vastly different (although either question is likely to be downvoted due to lack of evidence of research and the first would probably be closed as too broad too) – Damien_The_Unbeliever Aug 19 '19 at 15:50
  • 1
    An attribute is just a marker, it does not "possess" functions or triggers behavior by itself. So, maybe other libraries could make use of this attribute; it's most likely read by reflection, but that's your choice of reading or add NuGet packages that make use of it. Apart from Swagger, for a "simple" ASP.NET application, among the core Microsoft / ASP.NET packages I don't think it has any special use. (And by the way, Swagger is not part of basic MS packages as far as I know) – Pac0 Aug 20 '19 at 16:26
  • 1
    Thank you @Pac0 ! That was my initial concern. I knew it had some functionality for swagger/swashbuckle, which is a nugget package, but I was wondering if it had some functionality for ASP.NET, which you answered! – SpiritBob Aug 20 '19 at 16:47
  • I think this is a useful question. I too am about to add this to my API and I too was afraid that adding it might have some unintended consequences. So this was helpful to me. – Neve Dec 01 '20 at 07:34

3 Answers3

30

When applied to a public method on a controller, it prevents that method from appearing in the swagger ui.

Peter Marshall
  • 1,231
  • 1
  • 13
  • 22
7

First of all, to clarify, an attribute in C# does not trigger anything by itself. External code searches for classes, methods or properties marked with a specific attribute, and act accordingly.

Of course, there are many building blocks in ASP.NET MVC, it can be confusing sometimes.

This attribute is used by Swagger to hide the endpoint.

Also usedd (in .NET core at least) by the given implementations of IApiDescriptionProvider and other related interfaces, but that would be effective only if you actually use them (by configuring them up in Startup.cs)

(for some more details and example, see https://andrewlock.net/introduction-to-the-apiexplorer-in-asp-net-core/)

Pac0
  • 21,465
  • 8
  • 65
  • 74
0

This attribute helps to control the visibility. We can use this on the controller class or an action method when we want to hide that specific controller or action from showing in the swagger UI.