ASP.NET Core 2.2 基础知识(十五) Swagger
安装 Nuget 包
注册 Swagger
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
//注册 Swagger
services.AddSwaggerGen(sg =>
{
sg.SwaggerDoc("refuge", new Swashbuckle.AspNetCore.Swagger.Info
{
Title = "我的第一个 Swagger",
Version = "版本1"
});
});
}
启用 Swagger
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ //启用 Swagger
app.UseSwagger();
app.UseSwaggerUI(s =>
{
s.SwaggerEndpoint("/swagger/refuge/swagger.json", "My API 1.0.1");//注意,中间那段的名字 (refuge) 要和 上面 SwaggerDoc 方法定义的 名字 (refuge)一样
s.RoutePrefix = string.Empty; //默认值是 "swagger" ,需要这样请求:https://localhost:44384/swagger
}); ......
}
修改 launchSetting.json 文件中指定的默认启动路径
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56872",
"sslPort":
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
//"launchUrl": "api/values",
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SwashbuckleDemo2": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
CTRL+F5 启动项目
下面,我们来丰富一下这个 Swagger
新建一个 PersonsController
编辑该项目属性:
给 Action 添加 XML 注释:
添加 Swagger 的XML注释提示功能
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
//注册 Swagger
services.AddSwaggerGen(sg =>
{
sg.SwaggerDoc("refuge", new Swashbuckle.AspNetCore.Swagger.Info
{
Title = "我的第一个 Swagger",
Version = "版本1"
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
sg.IncludeXmlComments(xmlPath);
});
}
设置返回结果
Person 类如下:
/// <summary>
/// 人
/// </summary>
public class Person
{
/// <summary>
/// 编号
/// </summary>
public int Id { get; set; } /// <summary>
/// 姓名
/// </summary>
[Required(ErrorMessage = "姓名不能为空")] public string Name { get; set; } /// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
}
-----------------------------------------------------------------------------------
下面是回复网友的评论:
/// <summary>
/// 测试入参是实体,返回值也是实体
/// </summary>
/// <param name="person">人</param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(Person), )]//返个250给前端
public Person GetPerson([FromBody]Person person)
{
return person;
}
ASP.NET Core 2.2 基础知识(十五) Swagger的更多相关文章
- ASP.NET Core 2.2 基础知识(十八) 托管和部署 概述
为了方便演示,以 .NET Core 控制台应用程序讲解. 我们新建一个控制台应用程序,安装 "Newtonsoft.Json" Nuget 包,然后右键点击该项目,选择" ...
- ASP.NET Core 2.2 基础知识(十二) 发送 HTTP 请求
可以注册 IHttpClientFactory 并将其用于配置和创建应用中的 HttpClient 实例. 这能带来以下好处: 提供一个中心位置,用于命名和配置逻辑 HttpClient 实例. 例如 ...
- ASP.NET Core 2.2 基础知识(十六) SignalR 概述
我一直觉得学习的最好方法就是先让程序能够正常运行,才去学习他的原理,剖析他的细节. 就好像这个图: 所以,我们先跟着官方文档,创建一个 SignalR 应用: https://docs.microso ...
- ASP.NET Core 2.2 基础知识(十四) WebAPI Action返回类型(未完待续)
要啥自行车,直接看手表 //返回基元类型 public string Get() { return "hello world"; } //返回复杂类型 public Person ...
- ASP.NET Core 2.2 基础知识(十) Web服务器 - Kestrel
ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...
- ASP.NET Core 2.2 基础知识(十三) WebAPI 概述
我们先创建一个 WebAPI 项目,看看官方给的模板到底有哪些东西 官方给出的模板: [Route("api/[controller]")] [ApiController] pub ...
- ASP.NET Core 2.2 基础知识(十一) ASP.NET Core 模块
ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...
- ASP.NET Core 2.2 基础知识(九) 使用托管服务实现后台任务
在 ASP.NET Core 中,后台任务作为托管服务实现.托管服务是一个类,而且必须实现 IHostedService 接口,该接口定义了两个方法: StartAsync(CancellationT ...
- ASP.NET Core 2.2 基础知识(八) 主机 (未完待续)
主机负责应用程序启动和生存期管理.共有两个主机 API : 1.Web 主机 : 适用于托管 Web 应用,基于 IWebHostBuilder ; 2.通用主机 : 适用于托管非 Web 应用. 基 ...
随机推荐
- STL map、set中key为结构体的用法
下面是map定义的结构: // TEMPLATE CLASS map template<class _Kty, class _Ty, class _Pr = less<_Kty>, ...
- Codeforces Round #350 (Div. 2) C
C. Cinema time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- eclipse+jetty+web项目调试---不显示源码
本人eclipse版本:JUNO 1.问题现象:显示源码时,不显示箭头(指示到哪行) 解决办法: debug configurations --->Goals设置参数 clean -X je ...
- jw player学习笔记----跨域请求
需求来源:播放器皮肤文件请求不到,被限制了. 参考官网解决方案: http://www.longtailvideo.com/support/jw-player/28844/crossdomain-fi ...
- node读取文件夹名
const fs = require('fs'); const join = require('path').join; /** * * @param startPath 起始目录文件夹路径 * @r ...
- 创建 React 项目
依次输入命令: npm install -g create-react-app create-react-app react16 cd my-app npm start 在浏览器中输入 local:3 ...
- Web.xml过滤器配置及执行顺序概念
第一个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain ch ...
- 【洛谷 P2480】 [SDOI2010]古代猪文(中国剩余定理,Lucas定理)
题目链接 这题出的有点nb,PKU: Pig Kingdom University , NOIP: National Olympics in Informatic of Pigs... 题意:求\(G ...
- Csharp 非安全代码
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { c ...
- Python阶段复习 - part 2 - Python序列/持久化
1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中 >>> import json >>> imp ...