asp.net-core - Asp.Net 核心 Swashbuckle 设置 operationId

标签 asp.net-core swagger swashbuckle

我如何设置 Swagger operationId Asp.Net Core 2.1 项目中的属性?根据 this我应该使用的帖子 SwaggerOperationAttribute但我在 Swashbuckle.AspNetCore 库中找不到它。还有一个 IOperationFilter

public interface IOperationFilter
{
    void Apply(Operation operation, OperationFilterContext context);
}
我找不到任何用于 swagger 生成目的的实现。

最佳答案

还有 2 个其他选项,而无需编写任何额外的代码或添加额外的依赖项,例如 Swashbuckle.AspNetCore.Annotations 选项 1 :基于约定 - SwaggerGen可以选择设置 CustomOperationIds .所以你可以简单地将它设置为使用 ControllerName_HttpMethod像这样:

services.AddSwaggerGen(c =>
{
    c.CustomOperationIds(e => $"{e.ActionDescriptor.RouteValues["controller"]}_{e.HttpMethod}");
    c.SwaggerDoc("v1", new Info { Title = "Test API", Version = "v1" });
});
这会将 operationIds 添加到您的所有方法中,遵循 ControllerName_HttpMethod习俗。
选项 2 : ActionFilter/Attribute based - 您可以配置每个 Action 方法(就像您对 SwaggerOperation 操作过滤器所做的一样,只需将 Name 属性添加到您的 HTTP 动词操作过滤器,如下所示:
[HttpPost(Name="Post_Person")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public async Task<ActionResult<Response>> PostAsync([FromBody]Request request)
{
    Response result = await _context.PostAsync(request);
    return Ok(result);
}
这与 [SwaggerOperation(OperationId = "Post_Person")] 完全一样但不需要EnableAnnotations

关于asp.net-core - Asp.Net 核心 Swashbuckle 设置 operationId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52262826/

相关文章:

asp.net - 如何从 Swashbuckle.AspNet 生成多个页面?

java - swagger中spring fox health路由显示多个HTTP方法

c# - Swashbuckle:Swagger UI:自定义 API 操作

c# - 为什么在 IIS 上的网站下作为应用程序托管时 swagger.json 找不到错误?

c# - 无法从程序集办公室 writer.process() 加载类型 'System.Data.OleDb.OleDbType'

c# - 在注入(inject)类中使用 HttpContextAccessor

json - Swagger如何描述JSON主体参数

c# - 将描述中的链接添加到 Swagger 中的其他操作(通过 Swashbuckle)

asp.net - ASP.NET Core 中的 HttpResponse.Filter 等效项

asp.net-mvc - 如何更改 .NET Core 中 SQL 数据库中的 FirstName 属性?