首页 技术 正文
技术 2022年11月19日
0 收藏 748 点赞 4,907 浏览 901 个字

依赖项——右键——管理NuGet程序包——浏览——输入以下内容

Install-Package Swashbuckle.AspNetCore -Pre

.net core webapi添加swagger

双击Properties——点击生成——勾选XML文档文件

.net core webapi添加swagger

双击Startup.cs——在ConfigureServices、Configure中添加以下内容:

ConfigureServices:

.net core webapi添加swagger

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "接口文档",
Description = "接口文档",
TermsOfService = "None",
Contact = new Contact { Name = "唐少", Email = "tang14455@163.com", Url = "" }
});

//设置swagger json和ui的注释路径。
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var xmlPath = Path.Combine(basePath, "WebApi_Core.xml");//同XML文档文件中名称相同
c.IncludeXmlComments(xmlPath);
// c.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数
});

.net core webapi添加swagger


.net core webapi添加swagger


Configure:

.net core webapi添加swagger

         // 使中间件能够将生成的Swagger作为JSON端点。
app.UseSwagger();
// 使中间件能够服务swagger-ui(HTML,JS,CSS等),指定Swagger JSON端点。
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "TwBusManagement API V1");
c.ShowExtensions();
});

.net core webapi添加swagger


.net core webapi添加swagger


双击Properties下launchSettings.json——更改launchUrl中值为swagger(默认打开Swagger帮助页):


.net core webapi添加swagger


运行测试


.net core webapi添加swagger

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852