为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
拉风的咖菲猫 2024-01-20 21:36:54
我刚刚在控制器中添加了另一个 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)
查看完整描述

3 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

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


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


[HttpPost("createMultiple")]

public IActionResult CreateCars(List<Car> cars) {}


[HttpPost()]

public IActionResult CreateCar(Car car) {}

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


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


查看完整回答
反对 回复 2024-01-20
?
UYOU

TA贡献1878条经验 获得超4个赞

使用以下代码解决该问题,

services.AddSwaggerGen(options =>
        {

            options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
        });


查看完整回答
反对 回复 2024-01-20
?
HUX布斯

TA贡献1876条经验 获得超6个赞

在我的代码中,我使用 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)



查看完整回答
反对 回复 2024-01-20
  • 3 回答
  • 0 关注
  • 40 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信