加载 API 定义失败,获取错误:未定义 /swagger/v1/swagger.json

Posted

技术标签:

【中文标题】加载 API 定义失败,获取错误:未定义 /swagger/v1/swagger.json【英文标题】:Failed to load API definition, Fetch error: undefined /swagger/v1/swagger.json 【发布时间】:2021-06-03 04:04:08 【问题描述】:

发生了这个错误,我在所有站点上尝试了所有可能的解决方案,但仍然无法正常工作 我尝试大摇大摆的操作 我尝试路线 我尝试 HttpGet("List/id") 没有什么对我有用 错误详情如下:

      An unhandled exception has occurred while executing the request.
      Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Ambiguous HTTP method for action - MandobX.API.Controllers.ShipmentOperationsController.GetShipmentOperations (MandobX.API). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0
         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) 

我的代码: 控制器动作:

//Get lists of drivers, regions and packagges type to create shipment operation
        [Route("Init")]
        [HttpGet]
        public async Task<IActionResult> InitShipment()
        
            ShipmentViewModel shipmentInitViewModel = new ShipmentViewModel
            
                Drivers = await _context.Drivers.Include(d => d.User).Include(d => d.Vehicle).ToListAsync(),
                PackageTypes = await _context.PackageTypes.ToListAsync(),
                Regions = await _context.Regions.ToListAsync()
            ;
            return Ok(new Response  Code = "200", Data = shipmentInitViewModel, Msg = "Task Completed Succesfully", Status = "1" );
        
        // GET: api/ShipmentOperations
        [SwaggerOperation("List")]
        [Route("List")]
        [HttpGet("List/userId")]
        public async Task<ActionResult<IEnumerable<ShipmentOperation>>> GetShipmentOperations()
        
            return await _context.ShipmentOperations.ToListAsync();
        

        [SwaggerOperation("Details")]
        [Route("Details")]
        [HttpGet("Details/id")]
        public async Task<ActionResult<ShipmentOperation>> GetShipmentOperation(string id)
        
            var shipmentOperation = await _context.ShipmentOperations.FindAsync(id);

            if (shipmentOperation == null)
            
                return NotFound();
            

            return shipmentOperation;
        

谁能帮我解决这个问题?

【问题讨论】:

【参考方案1】:

答案在这篇文章的 github 上 https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/982 解决方案是让路由注释负责生成的路由,如下所示:

        [Route("List/userId")]
        [HttpGet]
        public async Task<ActionResult<IEnumerable<ShipmentOperation>>> 
        GetShipmentOperations(string userId)
        
            return await _context.ShipmentOperations.ToListAsync();
        

        // GET: api/ShipmentOperations/5
        [Route("Details/id")]
        [HttpGet]
        public async Task<ActionResult<ShipmentOperation>> GetShipmentOperation(string id)
        
            var shipmentOperation = await _context.ShipmentOperations.FindAsync(id);

            if (shipmentOperation == null)
            
                return NotFound();
            

            return shipmentOperation;
        

【讨论】:

以上是关于加载 API 定义失败,获取错误:未定义 /swagger/v1/swagger.json的主要内容,如果未能解决你的问题,请参考以下文章

Blazor Swagger:无法加载 API 定义。获取错误:未定义 /swagger/v1/swagger.json

加载 xdebug.so 未定义符号失败:zend_empty_string

显示获取的数据问题:对象作为 React 子项/类型错误无效:未定义

获取“未捕获的参考错误:$ 未定义” - 脚本以正确的顺序加载 - 并且可以从控制台工作 [重复]

React Native - 渲染从 API 获取数据的屏幕时出现“未定义不是对象”错误

Bmap未定义,怎么会出现这个错误