简体   繁体   中英

Failed to load API definition, Fetch error: undefined /swagger/v1/swagger.json

this error occurred and I tries all the possible solutions on all sites and still not working I tries swaggeroperation I tries Route I tries HttpGet("List/{id}") nothing worked for me Error details are:

      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) 

My code: controllers Actions:

//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;
        }

Can anyone help me with that?

the answer was on github on this post https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/982 the solution is making the route annotation responsible for the generated route like this:

        [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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM