Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Core: Swashbuckle Set operationId Automatically on to Controller Action Method

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.

Currently linking Net Core 3 APIs with Angular Typescript.

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

The following answer works:

[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"'

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


1 Answers

Utilize this piece of code:

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