Swashbuckle手动设置operationId,使用相同动词进行多个操作 [英] Swashbuckle set manually operationId, multiple operations with same verb

查看:215
本文介绍了Swashbuckle手动设置operationId,使用相同动词进行多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道是否可以设置自定义operationid或命名约定,我的意思是我知道操作过滤器可以用operationId的生成方式覆盖

I need to know if it's possible to set up custom operationid, or a naming convention, I mean I know that operation filter can be overwritten the way how operationId is generated

https://azure.microsoft.com/zh-CN/documentation/articles/app-service-api-dotnet-swashbuckle-customize/

using Swashbuckle.Swagger;
using System.Web.Http.Description;

namespace Something
{
    public class MultipleOperationsWithSameVerbFilter : IOperationFilter
    {
        public void Apply(
            Operation operation,
            SchemaRegistry schemaRegistry,
            ApiDescription apiDescription)
        {
            if (operation.parameters != null)
            {
                operation.operationId += "By";
                foreach (var parm in operation.parameters)
                {
                    operation.operationId += string.Format("{0}",parm.name);
                }
            }
        }
    }
}

在SwaggerConfig.cs中

in SwaggerConfig.cs

 c.OperationFilter<MultipleOperationsWithSameVerbFilter>();

现在这对转换摇摇晃晃的描述很有帮助,请检查以下内容:

Now this is helpful transforming swagger description, check bellow:

很好,现在我遇到了另一个问题,与可能的情况类似的示例:在同一控制器上,我有两个端点

All good, now I endup with another problem, example similar with may cases: on same controller I have two endpoints

  • 邮寄:/customer boddy:{电子邮件,位置....}
  • 帖子:/customer/search boddy:{一个过滤器,随便}

该示例不是很正确(最后一个帖子应该是get),但仍然假定在这种特殊情况下无法更改webapi(用于分离的新控制器),我将尝试找出一种为每个生成不同的operationId的方法采取某种行动,但我的问题是:

The example is not quite correct (last post should be a get) but still le assume that webapi cannot be changed (new controller for separation) for this particular case I will try to figure out a way to generate operationId diffrent for each action somehow, but my question is this:

是否可以以某种方式装饰控制器的动作,例如 [JsonIgnore]或与[Route("customer/delete")]一起使用,以明确说明 operationId .

Is it possible to decorate somehow the controller actions similar with [JsonIgnore] or with [Route("customer/delete")], to be explicit about the operationId.

推荐答案

编辑此答案与Swashbuckle 5.6和.NET Framework有关.请阅读 mwilson的答案以了解Swashbuckle和.NET Core

EDIT This answer relates to Swashbuckle 5.6 and .NET Framework. Please read mwilson's answer for Swashbuckle and .NET Core

您可以使用Swashbuckle提供的SwaggerOperationAttribute.

You can use the SwaggerOperationAttribute provided by Swashbuckle for that.

[SwaggerOperation("get")]
public IEnumerable<Contact> Get()
{
    ....
}

[SwaggerOperation("getById")]
public IEnumerable<Contact> Get(string id)
{
    ...
}

您也可以使用该属性向操作中添加标签和方案.看看源代码

You can use that attribute to add tags and schemes to your operation as well by the way. Have a look at the source code

这篇关于Swashbuckle手动设置operationId,使用相同动词进行多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆