NuGet添加Swashbuckle.AspNetCore,在Startup.cs添加和启用中间件Swagger

public void ConfigureServices(IServiceCollection services)
{
#region Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1.0.0",
Title = "My Web API",
Description = "说明文档",
TermsOfService = "None",
Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "My.Web", Email = "My.Web@xxx.com", Url = "https://www.cnblogs.com/Zev_Fung/" }
});
});
#endregion
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
#region Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Web API V1");
});
#endregion
}

使用自带的Kestrel web调试,输入地址:http://localhost:<port>/swagger,默认跳转到https://localhost:<port>/swagger/index.html

Swagger提示报错:

Failed to load API definition.
Errors
Fetch errorInternal Server Error /swagger/v1/swagger.json

打开http://localhost:<port>/swagger/v1/swagger.json,提示错误

An unhandled exception occurred while processing the request.
NotSupportedException: Ambiguous HTTP method for action - xxxxx.Controllers.BooksController.Post (xxxxx). Actions require an explicit HttpMethod binding for Swagger 2.0
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry) Stack Query Cookies Headers
NotSupportedException: Ambiguous HTTP method for action - xxxxx.Controllers.BooksController.Post (xxxxx). Actions require an explicit HttpMethod binding for Swagger 2.0
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry)
System.Linq.Enumerable.ToDictionary<TSource, TKey, TElement>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(string documentName, string host, string basePath, string[] schemes)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) Show raw exception details

或者在控制台查看输出错误

大概意思是:不支持的异常:操作的HTTP方法不明确,方法需要必须指明请求方式

在方法加上 [HttpGet]、[HttpPost] 等,就可以通过 Swagger UI 查看 API

上面只是其中一种引起的Fetch errorInternal Server Error /swagger/v1/swagger.json原因,其他也是通过查看错误信息解决

Swagger使用的时候报错:Failed to load API definition的更多相关文章

  1. Eclipse启动 报错[Failed to load the JNI shared library jvm.dll

    准备要做java服务器,在安装开发环境时,启动Eclipse报错[Failed to load the JNI shared library jvm.dll] 研究了下,造成错误的原因是由于eclip ...

  2. 使用laravel-admin后台sdk报错Failed to load resource: net::ERR_CERT_AUTHORITY_INVALID、Provisional headers are shown

    报错Failed to load resource: net::ERR_CERT_AUTHORITY_INVALID请先确定自己的资源url是否可以确实访问到(地址正确与否.访问权限是否开启等) 若n ...

  3. junit单元测试报错Failed to load ApplicationContext,但是项目发布到tomcat浏览器访问没问题

    junit单元测试报错Failed to load ApplicationContext,但是项目发布到tomcat浏览器访问没问题,说明代码是没问题的,配置也没问题.开始时怀疑是我使用junit版本 ...

  4. Abp的swagger UI 出现Failed to load API definition.

    Abp 出现Failed to load API definition.如下图: 原因:本次出现的原因是Api里面的方法重名了(只是方法的参数不一样)(可能是controller中的auction上面 ...

  5. 安卓预览报错 Failed to load AppCompat ActionBar with unknown error

    报错信息 : Render ProblemFailed to load AppCompat ActionBar with unknown error. Failed to instantiate on ...

  6. 使用elasticsearch启动项目报错failed to load elasticsearch nodes 。。。。。No type specified for field [name]

    failed to load elasticsearch nodes .....No type specified for field [name]翻译: 加载ElasticSearch节点失败... ...

  7. mesos-master启动失败,报错Failed to load unknown flag 'quorum.rpmsave'

    [现象] mesos启动失败,查看mesos状态报错: [root@hps102 ~]# systemctl status mesos-master ● mesos-master.service - ...

  8. Linux安装U盘启动报错Failed to load ldlinux.c32

    报错信息 使用U盘安装linux无法正常启动 Start booting from USB device... SYSLINUX 5.10 EDD 2013-06-04 Copyright (C) 1 ...

  9. Hive 报错 Failed to load class "org.slf4j.impl.StaticLoggerBinder".

    打开hive报错 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaultin ...

随机推荐

  1. 相机测试camera报告的问题

    AE问题 整体偏亮 整体偏暗 高光过爆 暗处过暗 对比度低/高 亮度: 关注暗处过暗 高光过爆   对比度: 关注头发,衣服 对比度低照片会有好像一层薄薄的,发蒙 关注植物,会有灰色的 AWB问题 偏 ...

  2. 「雕爷学编程」Arduino动手做(21)——激光开关模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

  3. 使用包时,报 xxx.default is not a function

     最近做了一个导出功能,代码如下 import request from 'request-promise-native'; export default class Form { // 导出 @po ...

  4. springboot使用redis的keyspace notifications 实现定时通知

    简单定时任务解决方案:使用redis的keyspace notifications(键失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服务器上的reids最少要是2.8版本 ...

  5. 存储系列之 Linux ext2 概述

    引言:学习经典永不过时. 我们之前介绍过存储介质主要是磁盘,先介绍过物理的,后又介绍了虚拟的.保存在磁盘上的信息一般采用文件(file)为单位,磁盘上的文件必须是持久的,同时文件是通过操作系统管理的, ...

  6. LeetCode上的SQL练习

    184. 部门工资最高的员工 SELECT D. NAME Department, E. NAME Employee, E.Salary FROM -- 内连接两张查询表 Employee E INN ...

  7. javascript代码重构需要考虑的问题(一)

    1.将数组的长度进行存储,循环时就不用每次去读取一次数组长度,从而提升性能 例如: for (var i = 0, len = arr.length; i < len; i++) { //do ...

  8. dede列表页限制标题长度

    {dede:list pagesize ='10' titlelen="45"} <li><a href="[field:arcurl/]"& ...

  9. springboot连接数据源,配置数据库

    Spring官方DriverManagerDataSource的配置 修改yml #配置数据源的属性 spring: datasource: driver-class-name: com.mysql. ...

  10. html5拖动监听

    在拖动目标上触发事件 (源元素): ondragstart - 用户开始拖动元素时触发 ondrag - 元素正在拖动时触发 ondragend - 用户完成元素拖动后触发 释放目标时触发的事件: o ...