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 ...
随机推荐
- 59、常规控件(2)TextInputLayout-让EditText提示更加人性化
提示语用在显示. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...
- Android去掉标题的方法
我们写程序的时候经常要全屏显示或者不显示标题.比如我们做地图导航的时候就不要标题了,下面介绍三种方法来实现Android去掉标题. 第一种:也一般入门的时候经常使用的一种方法 在setContentV ...
- C语言函数重入
C语言函数重入 可重入函数:可以被中断的函数,即这个函数执行时,可以中断其执行,可以由一个或多个任务并发使用,而不比担心数据错误. 不可重入函数(不安全函数) 不能运行在多任务环境下,除非能保证互斥( ...
- Delphi 中窗口文件与无窗口Pas文件的区别 (MTM)
implementation {$R *.dfm} ---- 带窗口的 dfm -- 一般的 windows 窗口 {$R *.fmx} ---- 带窗口的 fmx -- 一般的 FireMonk ...
- 延迟任务和循环任务ScheduledExecutorService
public class ScheduledThreadPool { public static ScheduledExecutorService scheduledThreadPool = Exec ...
- [HackerRank] The Longest Common Subsequence
This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...
- 如何修改Myeclipse的JSP模板
先找到MyEclipse的安装目录, 再找到myeclipse/eclipse/plugins/com.genuitec.eclipse.wizards_5.1.0/templates/jsp (co ...
- centos7在vmware上无法上网
centos7在虚拟机中设置NAT后也无法上网! 首先激活网卡!打开桌面右键在终端中打开:cd /etc/sysconfig/network-scripts/ls 找到以ifcfg开头的,如ifcfg ...
- Flask(4)- flask请求上下文源码解读、http聊天室单聊/群聊(基于gevent-websocket)
一.flask请求上下文源码解读 通过上篇源码分析,我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__call__方法返回了app的wsgi_app(en ...
- Using Swift with Cocoa and Objective-C--在同个project中使用Swift和在同个project中
http://www.cocoachina.com/newbie/basic/2014/0605/8688.html watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...