.net8 Swashbuckle.AspNetCore WEBAPI 配置要点记录

1. 因Controllers中一个方法缺少 [HttpMethod]特性,导至Swagger报错,报错信息如下:

Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Ambiguous HTTP method for action - XXXX.GetXXXXController.幽幽x. Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0

解决办法:

检查所有Action,确保所有Action上面都有正确的[HttpMethod]特性。

或者在AddSwagger的时候设置一下,让Swagger忽略没有[HttpMethod]特性的Action,方法是在在Program.cs文件中把 builder.Services.AddSwaggerGen() 这行代码替换成

builder.Services.AddSwaggerGen(options =>

{

options.DocInclusionPredicate((name, api) => api.HttpMethod != null);

});

2. 允许一个ApiController中出现多个请求方法相同的Action

只需把该ApiController中 路由规则 [Route("api/[controller]")] 替换成 [Route("api/[controller]/[action]")] 即可。

相关推荐
SEO-狼术17 小时前
Include Scannable Barcodes in Reports
.net
qq_4312807020 小时前
工作经验总结:半导体上位机软件开发与互联网开发的不同
c#·.net
ironinfo21 小时前
.net 高并发服务性能瓶颈排查处理
性能优化·.net·grpc
回忆2012初秋21 小时前
【.Net】一文讲清楚SonnetDB 时序库的使用
.net
雪飞鸿2 天前
ArrayPoolWrapper简洁、安全的ArrayPool
c#·.net·.net core·原创
我是唐青枫2 天前
C#.NET MemoryMarshal 深入解析:零拷贝内存重解释、二进制读写与使用边界
c#·.net
步步为营DotNet2 天前
深入剖析.NET 11 中 Semantic Kernel 于智能后端集成的创新实践
前端·.net·easyui
喵叔哟2 天前
10.【.NET10 实战--孢子记账--产品智能化】--其余第三方包批量升级
人工智能·.net
武藤一雄2 天前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore