gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:25:20 26 4
gpt4 key购买 nike

我如何设置 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com