Manually set operationId to allow multiple operations with the same verb in Swashbuckle(手动设置 operationId 以允许在 Swashbuckle 中使用相同动词进行多次操作)
问题描述
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/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);
}
}
}
}
}
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
- Post: /customer boddy: {email, location....}
- Post: /customer/search boddy: {a filter, whatever}
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:
Is it possible to decorate somehow the controller actions similar with [JsonIgnore] or with [Route("customer/delete")], to be explicit about the operationId.
EDIT This answer relates to Swashbuckle 5.6 and .NET Framework. Please read mwilson's answer for Swashbuckle and .NET Core
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
这篇关于手动设置 operationId 以允许在 Swashbuckle 中使用相同动词进行多次操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:手动设置 operationId 以允许在 Swashbuckle 中使用相同动词进行多次操作


- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01