Adding Swagger to Web API project
Adding Swagger to Web API project. All source code for this series can be found here.
When you create a new ASP.NET Web API project, a nuget package called Microsoft ASP.NET Web Api Help Page is installed to generate help page content for the web APIs on your site. In my previous post Runscope and continuous integration, I used this to provide descriptions for the APIs. The help page package is a good start but it is lacking things like discoverability and live interactions. This is where Swagger comes to the rescue.
"Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability."


Adding Swagger to your Web API does not replace ASP.NET Web API help pages. You can have both running side by side, if desired.
Adding Swagger to Web Api Project
To add Swagger to an ASP.NET Web Api, we will install an open source project called Swashbuckle via nuget.
After the package is installed, navigate to App_Start in the Solution Explorer. You'll notice a new file called SwaggerConfig.cs. This file is where Swagger is enabled and any configuration options should be set here. 
Configuring Swagger
At minimum you'll need this line to enable Swagger and Swagger UI.
GlobalConfiguration.Configuration
.EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
.EnableSwaggerUi();
Start a new debugging session (F5) and navigate to http://localhost:[PORT_NUM]/swagger. You should see Swagger UI help pages for your APIs. 
Expanding an api and clicking the "Try it out!" button will make a call to that specific API and return results. Pretty cool! 

Enable Swagger to use XML comments
The minimum configuration is nice to get started but let's add some more customization. We can tell Swashbuckle to use XML comments to add more details to the Swagger metadata. These are the same XML comments that ASP.NET Help Pages uses.
First, enable XML documentation file creation during build. In Solution Explorerright-click on the Web API project and click Properties. Click the Build tab and navigate to Output. Make sure XML documentation file is checked. You can leave the default file path. In my case its bin\SwaggerDemoApi.XML 
Next, we need to tell Swashbuckle to include our XML comments in the Swagger metadata. Add the following line to SwaggerConfig.cs. Make sure to change the file path to the path of your XML documentation file.
c.IncludeXmlComments(string.Format(@"{0}\bin\SwaggerDemoApi.XML", System.AppDomain.CurrentDomain.BaseDirectory));
Full configuration, so far
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "SwaggerDemoApi");
c.IncludeXmlComments(string.Format(@"{0}\bin\SwaggerDemoApi.XML", System.AppDomain.CurrentDomain.BaseDirectory));
})
.EnableSwaggerUi();
Finally, if you haven't already, add XML comments to your Models and API methods. 

Run the project and navigate back to /swagger. You should see more details added to your API documentation. I've highlighted a few below with their corresponding XML comment. 
Under Response Class, click Model. You should see any XML comments added to your models. 
Describing Enums As Strings
My Superhero class contains an Enum property called Universe which represents which comic universe they belong to. 
By default, Swagger displays these Enum values as their integer value. IMO, this is not very descriptive. Let's change it to display the string representation.
Add the following line to SwaggerConfig.cs
c.DescribeAllEnumsAsStrings();
The full swagger configuration at this point
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "SwaggerDemoApi");
c.IncludeXmlComments(string.Format(@"{0}\bin\SwaggerDemoApi.XML",
System.AppDomain.CurrentDomain.BaseDirectory));
c.DescribeAllEnumsAsStrings();
})
.EnableSwaggerUi();
If I look at Swagger now, the Universe Enum values are displayed as strings. Much better! 
These are just a few of the many configuration options you can specify in Swashbuckle to create your Swagger metadata. I encourage you to review the other options on Swashbuckle's GitHub.
Swagger JSON file
What we've seen so far is a UI representation our API Swagger metadata. To see the actual "Swagger", navigate to the URL that is in the header of the Swagger UI documentation page. 
This is how your API is discoverable. The Swagger metadata can be used to tell other APIs how to interact with yours. You can also create a client library to interact with your API that can be distributed to customers/users/integration partners. More on that in Part III of this series.
Here is a sample of my Swagger metadata 
The Microsoft Azure team is currently in the process of including Swagger in their new Azure App Service, currently in Preview. I encourage you to watch the //build/ 2015 talk about Azure App Service Architecture with Scott Hanselman and Scott Hunter.
Source code for this series: github.com/billpratt/SwaggerDemoApi
COPY FROM: http://www.wmpratt.com/swagger-and-asp-net-web-api-part-1/
Adding Swagger to Web API project的更多相关文章
- NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
https://blog.rsuter.com/nswag-tutorial-integrate-the-nswag-toolchain-into-your-asp-net-web-api-proje ...
- how to create an asp.net web api project in visual studio 2017
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutoria ...
- 使用swagger实现web api在线接口文档
一.前言 通常我们的项目会包含许多对外的接口,这些接口都需要文档化,标准的接口描述文档需要描述接口的地址.参数.返回值.备注等等:像我们以前的做法是写在word/excel,通常是按模块划分,例如一个 ...
- 使用swagger实现web api在线接口文档(转载)
一.前言 通常我们的项目会包含许多对外的接口,这些接口都需要文档化,标准的接口描述文档需要描述接口的地址.参数.返回值.备注等等:像我们以前的做法是写在word/excel,通常是按模块划分,例如一个 ...
- Asp.Net MVC Web API 中Swagger教程,使用Swagger创建Web API帮助文件
什么是Swagger? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法 ...
- .Net Core3.1下使用Swagger搭建web api项目
前言:微软于前天发布.net core 3.1正式版,并将长期支持3.1.所以我听到这个消息后就急忙下载.net core 3.1的SDK和Runtime,应该是公司最先用3.1的攻城狮了
- ASP.NET Web API Help Pages using Swagger
Understanding the various methods of an API can be a challenge for a developer when building a consu ...
- 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件
作者:Sreekanth Mothukuru 2016年2月18日 本文旨在介绍如何使用常用的 Swagger 和 Swashbuckle 框架创建描述 Restful API 的交互界面,并为 AP ...
- ASP.NET Web API 文件產生器 - 使用 Swagger
转帖:http://kevintsengtw.blogspot.hk/2015/12/aspnet-web-api-swagger.html Swagger 是一套 API 互動文件產生器,使用 HT ...
随机推荐
- [深入浅出WP8.1(Runtime)]文本块(TextBlock)
4.3 文本块(TextBlock) 文本块(TextBlock)控件是用于显示少量文本的轻量控件,可以通过TextBlock呈现只读的文本,你可以把TextBlock控件理解为一种纯文本的展示控件. ...
- 通过data:image/png;base64把图片直接写在src里
从网上下了个源文件查看时候发现了引用图片的地址不是在本地上的,而是后面跟了一大串字符data:image/png;base64...查了一下资料分析如下: 关于用base64存储图片 网页上有些图片的 ...
- 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊 分块/LCT
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...
- 【BZOJ1901】Zju2112 Dynamic Rankings
Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是 ...
- InterBase数据库迁移到MySQL(说明)
刚刚到公司1周便接到了第一个需求,进过了几天的沟通明白了是从gbk文件中恢复InterBase数据库,然后再将恢复到数据库中的数据导出到远程的MySQL数据库中,拿到需求先分步去看问题了,问题大致可分 ...
- BZOJ1171: 大sz的游戏&BZOJ2892: 强袭作战
Description 大sz最近在玩一个由星球大战改编的游戏.话说绝地武士当前共控制了N个星球.但是,西斯正在暗处悄悄地准备他们的复仇计划.绝地评议会也感觉到了这件事.于是,准备加派绝地武士到各星球 ...
- C#注意事项及错误处理
1 使用到config文件配置数据库路径 ConfigurationManager.ConnectionStrings["dbPath"].ConnectionString; db ...
- Oracle中"行转列"的实现方式
在报表的开发当中,难免会遇到行转列的问题. 以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用"行转列": scott的emp的原始数据为: ...
- 最长公共子序列 LCS
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D 题目: Description In a few ...
- Number Sequence
Number Sequence A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...