.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]")] 即可。

相关推荐
余衫马2 小时前
Agent Skills 实战(.NET):理论 × 代码 × 企业案例
人工智能·.net·agent·skill·openclaw
CSharp精选营5 小时前
字符串拼接用“+”还是 StringBuilder?别再凭感觉写了
.net·stringbuilder·字符串拼接·csharp
.NET修仙日记5 小时前
Acme .NET 工具类库:一站式解决.NET开发高频场景问题
.net·nuget·acme·.net8.0·.net9.0·acme.net·.net10.0
.NET修仙日记6 小时前
Acme.ReturnOh:让.NET API返回值处理更优雅,统一响应格式一步到位
c#·.net·webapi
喵叔哟8 小时前
19-AIAgent智能代理开发
微服务·.net
唐青枫11 小时前
深入理解 C#.NET TaskScheduler:为什么大量使用 Work-Stealing
c#·.net
喵叔哟11 小时前
20-多模态AI应用开发
人工智能·微服务·.net
桑榆肖物11 小时前
.NET 10 Native AOT 在 Linux 嵌入式设备上的实战
java·linux·.net·aot
我是唐青枫12 小时前
深入理解 C#.NET Task.Run:调度原理、线程池机制与性能优化
性能优化·c#·.net