本文介绍了Net Core:Swashbuckle将operationId自动设置为控制器操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建现有API的Angular API服务代理时,我们试图覆盖Swashbuckle/Swagger IO CodeGen的命名约定,以适用于现有500多个控制器和相应的方法.

We are trying to override Swashbuckle/Swagger IO CodeGen naming conventions, when its creating Angular API Service Proxies, for existing 500+ controllers and the corresponding methods.

当前将Net Core 3 API与Angular Typescript链接.

Currently linking Net Core 3 APIs with 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"'

是否有一种方法可以在启动时循环遍历所有控制器和方法?名称应等于Controller中动作方法的名称.

Is there a way to loop through all controllers and methods in startup? Name should equal name of Action Method within Controller.

这可能是解决方案,但是,我需要操作值.

This maybe possible solution, However, I need the action value.

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

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

推荐答案

使用这段代码:

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

这篇关于Net Core:Swashbuckle将operationId自动设置为控制器操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:40