【问题标题】:Net Core: Swashbuckle Set operationId Automatically on to Controller Action MethodNet Core:Swashbuckle 设置 operationId 自动 on 到 Controller Action 方法
【发布时间】:2020-11-18 12:17:38
【问题描述】:

在为现有的 500 多个控制器和相应的方法创建 Angular API 服务代理时,我们正在尝试覆盖 Swashbuckle/Swagger IO CodeGen 命名约定。

目前正在将 Net Core 3 API 与 Angular Typescript 相关联。

https://stackoverflow.com/a/58567622/13889515

以下答案有效:

[HttpGet("{id:int}", Name = nameof(GetProductById))]
public IActionResult GetProductById(int id) // operationId = "GetProductById"'

[HttpGet("{id:int}", Name = "GetProductById")]
public IActionResult GetProductById(int id) // operationId = "GetProductById"'

有没有办法在启动时循环遍历所有控制器和方法?名称应与控制器中的操作方法的名称相同。

这也许是可能的解决方案,但是,我需要行动值。

return services.AddSwaggerGen(c =>
{
    c.CustomOperationIds(e => $"{e.ActionDescriptor.RouteValues["controller"]}_{e.HttpMethod}");

https://stackoverflow.com/a/54294810/13889515

【问题讨论】:

  • 我认为如果你想生活在冒险的一边,从技术上讲,你可以用Name = "[controller]_[action]" 进行巨大的搜索/替换,这将假设[Http(...)] 属性已经存在

标签: c# .net asp.net-core swagger swashbuckle


【解决方案1】:

利用这段代码:

return services.AddSwaggerGen(c =>
{
    c.CustomOperationIds(e => $"{e.ActionDescriptor.RouteValues["action"]}");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 2017-07-01
    • 2016-12-22
    • 2019-02-15
    • 2021-05-25
    • 2022-12-04
    • 2012-09-16
    • 1970-01-01
    相关资源
    最近更新 更多