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

相关推荐
人丰6 小时前
血泪复盘:一次由 HttpContext.Current 引发的 CPU 100% 惨案
性能优化·.net
ChaITSimpleLove9 小时前
用 .NET 10 技术栈 “玩转 AI 学习” 的路线图:从 Minimal API 到 AI Agent 产品
人工智能·.net·学习 ai
半亩码田1 天前
【.NET新特性·第14篇】.NET 10 运行时与 SDK 新特性
.net
祀爱1 天前
C# MQTT 连接服务
后端·c#·.net
足球中国2 天前
.net DataExcel控件 2.11.1.53版发布
c#·.net·excel
rockey6273 天前
C#脚本引擎之AScript与Jurassic、Jint对比
javascript·c#·.net·js·script·动态脚本
界面开发小八哥3 天前
界面控件DevExpress WinForms中文帮助文档 - 入门指南
ai·c#·.net·devexpress·ui开发·winforms
清风与日月3 天前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore
大鹏说大话3 天前
用 Semantic Kernel 在 .NET 中构建你的第一个 AI 智能体(Agent)
人工智能·.net
七仔啊3 天前
C#、.NET 、ASP.NET Core、Visual Studio全系列快速入门
c#·asp.net·.net