c# - 加载 API 定义失败,获取错误:未定义 /swagger/v1/swagger.json
问题描述
发生了这个错误,我在所有站点上尝试了所有可能的解决方案,但仍然无法正常工作
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;
}
任何人都可以帮助我吗?
解决方案
答案在这篇文章的 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;
}
推荐阅读
- python - 如何以及在何处将 .lower() 放置在具有多个条件的代码中?
- python - 多个数据帧 - 使用熊猫从宽到长
- scikit-learn - 如何在 sklearn 中使用 GaussianNB 和 KNeighborsRegressor 的类权重?
- inext - 计算需要多少额外个体才能对样本/区域的 100% 渐近物种丰富度进行采样的最佳方法 - iNEXT?
- html - btn-group 的引导间距问题
- excel - 使用条件格式过滤数据或将其复制到其他地方
- iis - IIS中基于URL将域名绑定到网站
- reactjs - 我在 Next.js 中的动态路由的动态 manifest.json
- python - 如何使用 Python 从 Jupyter Notebook 将 Plotly 生成的交互式图形导出为 HTML 文件?
- xero-api - 新的 API 更新返回的代码 BADDEBT、CAPIMPORTS、CIUINPUT 与 TaxType 或 ReportTaxType 的 API 状态不匹配