博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET Core WebApi使用Swagger生成API说明文档【特性版】
阅读量:4565 次
发布时间:2019-06-08

本文共 4235 字,大约阅读时间需要 14 分钟。

⒈新建ASP.NET Core WebAPi项目

⒉添加 NuGet 包

Install-Package Swashbuckle.AspNetCore

⒊Startup中配置

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Builder; 6 using Microsoft.AspNetCore.Hosting; 7 using Microsoft.AspNetCore.Mvc; 8 using Microsoft.Extensions.Configuration; 9 using Microsoft.Extensions.DependencyInjection;10 using Microsoft.Extensions.Logging;11 using Microsoft.Extensions.Options;12 using Swashbuckle.AspNetCore.Swagger;13 14 namespace SwaggerDemo15 {16     public class Startup17     {18         public Startup(IConfiguration configuration)19         {20             Configuration = configuration;21         }22 23         public IConfiguration Configuration { get; }24 25         // This method gets called by the runtime. Use this method to add services to the container.26         public void ConfigureServices(IServiceCollection services)27         {28             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);29 30             //注册Swagger生成器,定义一个和多个Swagger 文档31             services.AddSwaggerGen(option =>32             {33                 //配置第一个Doc34                 option.SwaggerDoc("v1", new Info35                 {36                     Version = "v1",37                     Title = "My API_1"38                 });39             });40         }41 42         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.43         public void Configure(IApplicationBuilder app, IHostingEnvironment env)44         {45             if (env.IsDevelopment())46             {47                 app.UseDeveloperExceptionPage();48             }49 50             //启用中间件服务生成Swagger作为JSON终结点51             app.UseSwagger();52 53             //启用中间件服务对swagger-ui,指定Swagger JSON终结点54             app.UseSwaggerUI(c =>55             {56                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "DemoAPI V1");57                 //c.RoutePrefix = "swagger";  //默认58                 c.RoutePrefix = string.Empty;59             });60 61             app.UseMvc();62         }63     }64 }

⒋添加 特性相关NuGet 包

1 Install-Package Swashbuckle.AspNetCore.Annotations

⒌修改Startup中的ConfigureServices方法,启用特性支持

1         public void ConfigureServices(IServiceCollection services) 2         { 3             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 4  5             //注册Swagger生成器,定义一个和多个Swagger 文档 6             services.AddSwaggerGen(option => 7             { 8                 //配置第一个Doc 9                 option.SwaggerDoc("v1", new Info10                 {11                     Version = "v1",12                     Title = "My API_1"13                 });14                 //启用特性支持15                 option.EnableAnnotations();16             });17         }

⒍一些示范

 

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Http; 6 using Microsoft.AspNetCore.Mvc; 7 using SwaggerDemo.Models; 8 using Swashbuckle.AspNetCore.Annotations; 9 10 namespace SwaggerDemo.Controllers11 {12     [Route("api/[controller]")]13     [ApiController]14     [SwaggerTag("用户控制器")]15     public class UserController : ControllerBase16     {17         [HttpGet("get")]18         [SwaggerOperation(Summary = "获取用户列表",Description = "获取系统中所有用户信息",OperationId = "GetUsers")]19         public IList
GetUsers()20 {21 return new List
22 {23 new User{ id = 1,username = "fanqi",password = "admin",age = 25,email = "fanqisoft@163.com"},24 new User{ id = 2,username = "gaoxing",password = "admin",age = 22,email = "gaoxing@163.com"}25 };26 }27 28 [SwaggerResponse(201, "用户创建成功"]29 [SwaggerResponse(400, "用户创建失败")]30 [SwaggerOperation(Summary = "新建用户信息", Description = "新建用户信息", OperationId = "CreateUser")]31 [HttpPost("add")]32 public IActionResult CreateUser([FromForm, SwaggerParameter("新建用户数据", Required = true)]User user)33 {34 return Ok();35 }36 }37 }

 

转载于:https://www.cnblogs.com/fanqisoft/p/10956951.html

你可能感兴趣的文章
Paint Chain HDU - 3980(sg)
查看>>
Chales常用操作
查看>>
C++ 运算符重载<<
查看>>
windows镜像
查看>>
Flask 模板语法
查看>>
ZOJ FatMouse' Trade 贪心
查看>>
音乐播放器
查看>>
SQL COOKBOOK (Ch.1-10)
查看>>
创建数组
查看>>
dict使用
查看>>
[转] 移动平台Html5的viewport使用经验
查看>>
ASP.NET MVC的帮助类HtmlHelper和UrlHelper
查看>>
《Python数据科学手册》第五章机器学习的笔记
查看>>
ubuntu16.04 配置爬虫环境
查看>>
Centos7,PHP7安装swoole
查看>>
02_ListActive中响应事件 并LogCat输出
查看>>
doubleclick adx note
查看>>
Celery框架
查看>>
[c#]asp.net开发微信公众平台(4)关注事件、用户记录、回复文本消息
查看>>
[转载,感觉写的非常详细]DUBBO配置方式详解
查看>>