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:
- Open Visual Studio and create a new Web API project (either Global.asax-, OWIN- or ASP.NET Core-based).
- Register the NSwag Swagger middlewares:
- Global.asax: Register the OWIN Middlewares in your Global.asax.
- OWIN and ASP.NET Core: Register the OWIN or ASP.NET Core Middlewares in your Startup.cs.
- (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:
- Install NSwagStudio.
- Create a new C# library project in your solution (the service client library).
- Add the required assembly dependencies to the library project.
Generate code:
- Start NSwagStudio and select “Swagger Specification” as input.
- Enter the Swagger specification URL (default:
http://yourserver/swagger/v1/swagger.json
, the server must be running). - Click “Generate Outputs” and select the tab “CSharpClient”.
- Copy the source code into your client project.
- 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:
- Save the
.nswag
file in NSwagStudio - Open a command prompt and
cd
to the directory with the.nswag
file - Run
nswag run
to execute all.nswag
files (the NSwag command line tool is automatically registered by the MSI installer)
NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project的更多相关文章
- NSwag在asp.net web api中的使用,基于Global.asax
https://github.com/NSwag/NSwag/wiki/OwinGlobalAsax This page explains how to use the NSwag OWIN midd ...
- nswag vs swashbuckle
https://www.reddit.com/r/dotnet/comments/a2181x/swashbuckle_vs_nswag/ Swashbuckle https://github.com ...
- Getting Started with ASP.NET Web API 2 (C#)
By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print Download Com ...
- webAPI---发布(IIS)--发布遇到问题(500.19,500.21,404.8,404.3)
WebAPI的内容部分直接转自官方文档,英语水平有限,不做翻译, 发布网站在本文的后半部分 HTTP is not just for serving up web pages. It is also ...
- Unity文档阅读 第三章 依赖注入与Unity
Introduction 简介In previous chapters, you saw some of the reasons to use dependency injection and lea ...
- [转]Getting Started with ASP.NET Web API 2 (C#)
http://www.asp.net/web-api 本文转自:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web- ...
- NSwag给api加上说明
参考http://petstore.swagger.io 给controller加上description https://github.com/RSuter/NSwag/issues/1803 xm ...
- Microsoft Azure Tutorial: Build your first movie inventory web app with just a few lines of code
Editor’s Note: The following is a guest post from Mustafa Mahmutović, a Microsoft Student Partner wh ...
- Tutorial 1: Serialization
转载自:http://www.django-rest-framework.org/tutorial/1-serialization/#tutorial-1-serialization Tutorial ...
随机推荐
- 【BZOJ2055】80人环游世界 有上下界费用流
[BZOJ2055]80人环游世界 Description 想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么 一个 ...
- CodeForces 666A Reberland Linguistics(DP)
A. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Powershell Function Get-TimeZone
代码原文地址: https://gallery.technet.microsoft.com/scriptcenter/Get-TimeZone-PowerShell-4f1a34e6 <# .S ...
- cocos2d 场景切换和弹出场景、收回场景
场景弹出收回很简单 用以下代码在任意一个地方显示“设置场景”: [[CCDirector sharedDirector] pushScene:[Settings scene]]; 如果你身处“设置场景 ...
- java URL 利用网址api 查出手机号归属地
手机号码归属地查询api接口 1.淘宝网API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=手机号码参数:tel:手机号码返 ...
- Scrapy框架-scrapy框架架构详解
1.Scrapy框架介绍 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自己从零开始写的话,比较浪费 ...
- SVN入门-2分钟教你入门
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010540106/article/details/37317201 学习SVN首先我们应该知道 ...
- Pytorch(一)
一.Pytorch介绍 Pytorch 是Torch在Python上的衍生物 和Tensorflow相比: Pytorch建立的神经网络是动态的,而Tensorflow建立的神经网络是静态的 Tens ...
- mysql进阶(二)之细谈索引、分页与慢日志
索引 1.数据库索引 数据库索引是一种数据结构,可以以额外的写入和存储空间为代价来提高数据库表上的数据检索操作的速度,以维护索引数据结构.索引用于快速定位数据,而无需在每次访问数据库表时搜索数据库表中 ...
- Library Cache优化与SQL游标
Library Cache主要用于存放SQL游标,而SQL游标最大化共享是Library Cache优化的重要途径,可以使SQL运行开销最低.性能最优. 1 SQL语句与父游标及子游标 在PL/SQL ...