https://blog.rsuter.com/nswag-tutorial-integrate-the-nswag-toolchain-into-your-asp-net-web-api-project/

This tutorial shows how to integrate NSwag (Swagger toolchain for .NET) so that you can access your Web API actions with the Swagger UI and a generated C# client library.

The toolchain can be integrated into the following three types of Web API projects:

  • Global.asax-based Web projects (full .NET Framework)
  • OWIN-based Web projects (full .NET Framework)
  • ASP.NET Core based projects (full .NET Framework or .NET Core)

For each project, the integration is a little bit different, but this tutorial will provide links to all supported ways. Just follow the steps.

1. Create and setup a new Web API project

First we implement your Web API project:

  1. Open Visual Studio and create a new Web API project (either Global.asax-, OWIN- or ASP.NET Core-based).
  2. Register the NSwag Swagger middlewares:
  3. (optional) Globally register the Web API exception serialization filter

2. Run the Web API project

Run the project and navigate to the URL http://yourserver/swagger: Here you should see the Swagger UI.

Try calling your Web API actions and check the results.

3. Generate client code to access the Web API

The next steps show how to generate a client library to access the Web API actions in the Web project. In this tutorial we use the C# client generator (there is also a TypeScript client generator).

Setup tooling and create client project:

  1. Install NSwagStudio.
  2. Create a new C# library project in your solution (the service client library).
  3. Add the required assembly dependencies to the library project.

Generate code:

  1. Start NSwagStudio and select “Swagger Specification” as input.
  2. Enter the Swagger specification URL (default: http://yourserver/swagger/v1/swagger.json, the server must be running).
  3. Click “Generate Outputs” and select the tab “CSharpClient”.
  4. Copy the source code into your client project.
  5. Now you should change generator settings in the NSwagStudio to improve and customize the generated code (e.g. define the correct namespace, etc.).

Automate script generation:

  1. Save the .nswag file in NSwagStudio
  2. Open a command prompt and cd to the directory with the .nswag file
  3. Run nswag run to execute all .nswag files (the NSwag command line tool is automatically registered by the MSI installer)

最新文章

  1. MVC VS2012 Code First 数据库迁移教程
  2. 抽象类和接口 static final finalize
  3. 【温故而知新-Javascript】对象
  4. 用C#进行WinForm开发对数据库的相关操作
  5. oracle中怎么查看存储过程的源码
  6. Innodb中的锁
  7. 自学Zabbix3.6.3-触发器triggers expression表达式
  8. MySQL 的数据目录
  9. Promise学习笔记
  10. 20181207_Second_小结
  11. Windows Server 2016-域站点复制查询
  12. 【M2】软件工程终期总结报告——前端设计总结
  13. 最简单的方式离线部署Python依赖包
  14. EOS使用
  15. Oracle导出csv时数字不变成科学计数法
  16. 奇妙的 CSS shapes(CSS图形) 【css 图形,绘图,CSS 几何图形)】
  17. 表单如何与Servlet控制器关联
  18. STL容器基本功能与分类
  19. 初学Objective - C语法之代码块(block)
  20. 使用AChartEngine画动态曲线图

热门文章

  1. CSS-微信开放UI样式
  2. Lisp语言简介
  3. 自己根据java的LinkedList源码编写的一个简单的LinkedList实现
  4. XMLHttpRequest 对象 status 和statusText 属性对照表
  5. Leetcode-Combinations Sum II
  6. Less-mixin判断(守卫)一
  7. MySQL 1067
  8. delphi -----获取计算IP
  9. MySQL数据库的设计和表创建
  10. PyNest——part 3: connecting networks with synapses