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 ...
随机推荐
- Dapper的语法应用
(1)返回某个整型或字符串类型的字段 public string GetSupplierCodeById(int Id) { using( var conn=DbFactory.CreateConne ...
- Google I/O 2013 – Volley: Easy, Fast Networking for Android
1.什么是volley Volley是Ficus Kirpatrick在Gooogle I/O 2013发布的一个处理和缓存网络请求的库,能使网络通信更快,更简单,更健壮.Volle ...
- FZU1465
题目链接:传送门 题目大意:给你n个整数(可正可负),求有多少个连续的子序列的和==m(时限1S) 题目思路:前缀和+手写hash(map效率太慢,会超时) 具体做法是用一个数组sum,数组的第i位保 ...
- dubbo 序列化 问题 属性值 丢失 ArrayList 解决
参考文章:http://blog.csdn.net/wanyanxgf/article/details/6944733 http://tianya23.blog.51cto.com/1081650/5 ...
- 转载: Flex 布局教程
demo:页面二等分 .flex-box { display: -webkit-flex; /* Safari */ display: flex; flex-direction: row; justi ...
- postgresql常用
postgresql 字符串转整数 int.integer --把'1234'转成整数 select cast('1234' as integer ) ; --用substring截取字符串,从第8个 ...
- 使用maven搭建SSM框架
使用maven搭建SSM框架,首先得准备好maven环境. 搭建maven环境 第一步:下载maven http://maven.apache.org/download.cgi 下载后解压就可以了. ...
- 在Mac OS X使用Elasticsearch的基本流程
这篇日志的目的非常easy,就是记录一些主要的流程.要在OS X上使用Elasticsearch,事实上非常easy,在这里:https://www.elastic.co/downloads/elas ...
- tomcat 介绍
Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发 ...
- Git学习笔记-完全版
注意本文参考廖雪博客: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 一:Git ...