仅在控制器中添加另一个操作方法就会导致 Swashbuckle 崩溃

Tro*_*ing 3 c# asp.net-web-api swagger swashbuckle asp.net-core

我刚刚在控制器中添加了另一个 post 方法,Swagger Swashbuckle 崩溃了。如何解决这个问题?

 [HttpPost]
        public IActionResult CreateCars(List<Car> cars)
        {
            _carService.CreateCars(cars);
            return NoContent();
        }

System.NotSupportedException: HTTP method "POST" & path "api/Cars" overloaded by actions - IrkcnuApi.Controllers.CarsController.Create (WebApi),MyWebAPI.Controllers.CarsController.CreateCars (MyWebApi). Actions require unique method/path combination for OpenAPI 3.0. Use ConflictingActionsResolver as a workaround
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Run Code Online (Sandbox Code Playgroud)

Fre*_*els 5

您的控制器中已经有一个带有属性的方法HttpPost。由于您没有明确指定路线,因此这些操作会发生冲突。

您可以通过为这些 POST 操作指定路由来解决此问题,例如:

[HttpPost("createMultiple")]
public IActionResult CreateCars(List<Car> cars) {}

[HttpPost()]
public IActionResult CreateCar(Car car) {}
Run Code Online (Sandbox Code Playgroud)

上面的建议当然不是“RESTfull”,因为你的 URL 中有动词。

我建议修改您的代码,以便您只有一个“创建”方法,因为上述两个操作实际上隐式相同(我猜)。使用CreateCars仅包含一项的汽车集合调用该操作在某种意义上实际上与调用该CreateCar操作相同。