swagger报错: version fields are swagger: "2.0" and those that match openapi: 3.0.n
一、swagger配置
startup中Configure
app.UseSwagger(c => { c.RouteTemplate = "api-doc/{documentName}/swagger.json"; }); app.UseSwaggerUI(c => { c.RoutePrefix = "api-doc"; c.SwaggerEndpoint("v1/swagger.json", "Lampblack Api v1"); }); app.UseRouting();
launchsettings.json配置:
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:6001", "sslPort": 6001 } }, "profiles": { "YiSha.Admin.AppletApi": { "commandName": "Project", "dotnetRunMessages": "true", "launchBrowser": true, "launchUrl": "api-doc", "applicationUrl": "http://localhost:6001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
二、swagger报错解决
网上有如下两个解决方法:
- 一、加[HttpPost]或者 [HTTPGet]
api中的Controller/Action 加入[HttpPost]或者 [HTTPGet]
- 二、代码问题修改
两个不同的函数名称配成了同一个api名称
但实际我这都不是这个问题,问题是中间件的位置放的不对。
三、中间件的位置
app.UseSwaggerUI
要放在app.UseSwagger 后面。放在 app.UseRouting();前面
app.UseSwagger 要放在 如下两个后面
app.UseAuthentication();
// 自定义认证中间件
app.UseMiddleware<JwtTokenAuth>();
- 四、路由名称问题