小编Sil*_*mor的帖子

Swashbuckle设置manualy operationId

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

https://azure.microsoft.com/en-us/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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在SwaggerConfig.cs中

 c.OperationFilter<MultipleOperationsWithSameVerbFilter>();
Run Code Online (Sandbox Code Playgroud)

现在这有助于改变招摇的描述,请检查以下内容:

在此输入图像描述

一切都很好,现在我最终在一个更黑暗的地方,例如类似于可能的情况:在同一个控制器上我有两个端点

  • 发布:/ customer boddy:{email,location ....}
  • 发布:/ customer/search boddy:{a filter,whatever}

这个例子不太正确(最后一篇文章应该是get)但仍然假设webapi无法更改(新的控制器用于分离)对于这个特殊情况我会试着找出一种方法来为每个动作以某种方式生成operationId diffrent,但我的问题是:

是否有可能以某种方式装饰与[JsonIgnore]或[Route("customer/delete")]类似的控制器动作,以明确有关operationId的信息.

c# asp.net-web-api swagger asp.net-web-api-routing swashbuckle

12
推荐指数
2
解决办法
8096
查看次数