How can I set swagger operationId
attribute in Asp.Net Core 2.1 project? According to this post I should use SwaggerOperationAttribute
but I cannot find it in Swashbuckle.AspNetCore library. Also there is an IOperationFilter
public interface IOperationFilter
{
void Apply(Operation operation, OperationFilterContext context);
}
and I can't find any implementations for swagger generation purposes.
You can enable annotation on swagger with the Swashbuckle.AspNetCore.Annotations nuget package. (https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md#swashbuckleaspnetcoreannotations)
Once annotations have been enabled, you can enrich the generated Operation metadata by decorating actions with a SwaggerOperationAttribute.
There are 2 other options without having to write any extra code or add extra dependency like
Swashbuckle.AspNetCore.Annotations
Option 1: Convention based -
SwaggerGen
has an option to setCustomOperationIds
. So you can simply set it to useControllerName_HttpMethod
like this:This will add operationIds to all your methods, following
ControllerName_HttpMethod
convention.Option 2: ActionFilter/Attribute based - you can configure each Action method (as you'd do with
SwaggerOperation
action filter by simple adding aName
property to your HTTP verb action filter like this:This works exactly like
[SwaggerOperation(OperationId = "Post_Person")]
but without the need ofEnableAnnotations
Finally, I came to this solution:
But it still seems to me that I've reinvented the wheel.