Asp.Net Core WebApi中接入Swagger组件(初级)
2018-08-18 10:26
1026 查看
开发WebApi时通常需要为调用我们Api的客户端提供说明文档。Swagger便是为此而存在的,能够提供在线调用、调试的功能和API文档界面。
环境介绍:Asp.Net Core WebApi + Swagger。
一、加入Swagger组件
Swashbuckle.AspNetCore 中分为3个组件。
Package | Description |
---|---|
Swashbuckle.AspNetCore.Swagger | Exposes SwaggerDocument objects as a JSON API. It expects an implementation of ISwaggerProvider to be registered which it queries to retrieve Swagger document(s) before returning as serialized JSON |
Swashbuckle.AspNetCore.SwaggerGen | Injects an implementation of ISwaggerProvider that can be used by the above component. This particular implementation automatically generates SwaggerDocument(s) from your routes, controllers and models |
Swashbuckle.AspNetCore.SwaggerUI |
Exposes an embedded version of the swagger-ui. You specify the API endpoints where it can obtain Swagger JSON and it uses them to power interactive docs for your API、 |
安装Swashbuckle.AspNetCore,在Nuget中搜索或是包管理器下输入命令都可,找到这个包,安装,其中有三个小弟是三个不同的组件,在第一个中都集成了。
二、加入到Asp.Net Core管道中,并注入服务
加入Swagger中间件
1 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 2 { 3 if (env.IsDevelopment()) 4 { 5 app.UseDeveloperExceptionPage(); 6 } 7 8 app.UseSwagger(); 9 app.UseSwaggerUI(c => 10 { 11 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); 12 }); 13 14 app.UseMvc(); 15 }
加入Swagger服务
1 public void ConfigureServices(IServiceCollection services) 2 { 3 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); 4 5 services.AddSwaggerGen(options => 6 { 7 options.SwaggerDoc("v1", new Info { Title = "My WebApi Document", Version = "v1" }); 8 }); 9 }
三、启动WebApi即可,注意访问路径
2018-08-18,望技术有成后能回来看见自己的脚步
相关文章推荐
- 在ASP.NET Core Web API上使用Swagger提供API文档
- Asp.net core WebApi 使用Swagger生成帮助页实例
- asp.net core webapi 使用swagger
- ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了
- 在ASP.NET Core Web API上使用Swagger提供API文档
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
- Asp .Net Core Web API的swagger 使用
- 在ASP.NET Core Web API上使用Swagger提供API文档
- Asp.net core WebApi 使用Swagger生成帮助页实例
- 使用Swagger来生成asp.net core Web API 文档
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
- 在ASP.NET Core Web API上使用Swagger提供API文档
- asp.net core webapi swagger 使用二
- Asp.net Core WebApi 使用Swagger做帮助文档,并且自定义Swagger的UI
- Using MongoDB with Web API and ASP.NET Core
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- Asp.NetCoreWebApi图片上传接口(二)集成IdentityServer4授权访问(附源码)
- ASP.NET Core WebAPI 开发-新建WebAPI项目
- Asp.net Core WebApi 全局异常类
- ASP.Net Core WebApi几种版本控制对比