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

我刚刚在控制器中添加了另一个 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)



拉风的咖菲猫
浏览 47回答 3
3回答

SMILET

您的控制器中已经有一个带有属性的方法HttpPost。由于您没有明确指定路线,因此这些操作会发生冲突。您可以通过为这些 POST 操作指定路由来解决此问题,例如:[HttpPost("createMultiple")]public IActionResult CreateCars(List<Car> cars) {}[HttpPost()]public IActionResult CreateCar(Car car) {}上面的建议当然不是“RESTfull”,因为你的 URL 中有动词。我建议修改您的代码,以便您只有一个“创建”方法,因为上述两个操作实际上隐式相同(我猜)。使用CreateCars仅包含一项的汽车集合调用该操作在某种意义上实际上与调用该CreateCar操作相同。

UYOU

使用以下代码解决该问题,services.AddSwaggerGen(options&nbsp;=> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.ResolveConflictingActions(apiDescriptions&nbsp;=>&nbsp;apiDescriptions.First()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});

HUX布斯

在我的代码中,我使用 Swagger Swashbuckle 5.5.1 和 Microsoft.AspNetCore.Mvc.Versioning 4.1.1 对于我来说 [ApiExplorerSettings(GroupName = "vx.0")] 其中 x 是同一控制器或其他控制器中多个操作的版本,工作正常。我还同时使用 MapToApiVersion 属性,但属性 ApiExplorerSettings 避免冲突。请参阅https://www.myget.org/feed/domaindrivendev/package/nuget/Swashbuckle.AspNetCore.Swagger 的“装饰单个操作”在我的测试中我有 2 个控制器。第一个控制器映射版本 1.0 和 2.0。仅第二个控制器地图版本 3.0第一控制器:[Authorize][ApiVersion("1.0")][ApiVersion("2.0")][Route("viewqlikapi")][Route("ViewQlikApi/v{version:apiVersion}")][ApiController]public class QlikController : ControllerBase, IQlikController两个具有相同路径的操作 // Dati Pratica Audit[HttpGet][ApiExplorerSettings(GroupName = "v1.0")][Route("datipraticaaudit")]public RisultatoElementiPagina<ViewQlikDatiPraticaAudit GetElementiPaginaDatiPraticaAudit(int numeroElementi, int indicePagina)....[HttpGet][MapToApiVersion("2.0")][ApiExplorerSettings(GroupName = "v2.0")][Route("datipraticaaudit")]public RisultatoElementiPagina<ViewQlikDatiPraticaAudit> GetElementiPaginaDatiPraticaAuditV2(int numeroElementi, int indicePagina, int other)...和第二个控制器..[Authorize][ApiVersion("3.0")][Route("ViewQlikApi/v{version:apiVersion}")][ApiController]public class QlikV2Controller : ControllerBase和行动[HttpGet][MapToApiVersion("3.0")][ApiExplorerSettings(GroupName = "v3.0")][Route("auditoperativoaccesso")]public RisultatoElementiPagina<ViewQlikAuditOperativoAccesso> GetElementiPaginaAuditOperativoAccesso(int numeroElementi, int indicePagina, int o)
打开App,查看更多内容
随时随地看视频慕课网APP