Before moving forward, let’s learn what NSwag is.
What is NSwag?
NSwag is a Swagger/OpenAPI 2.0 and 3.0 toolchain for .NET, .NET Core, Web API, ASP.NET Core, TypeScript (jQuery, AngularJS, Angular 2+, Aurelia, KnockoutJS and more) and other platforms, written in C#. The Swagger specification uses JSON and JSON Schema to describe a RESTful Web API. The NSwag project provides tools to generate Swagger specifications from existing ASP.NET Web API controllers and client code from these Swagger specifications.
The definition is a reference from the below link: https://github.com/RSuter/NSwag
What is OpenAPI?
In short, OpenAPI is a project which looks at standardizing how REST APIs are described.
As an example, if you take WSDL which is SOAP-based, it has standards. When you create a .asmx service, its document is generated in the form of XML which tells how to access that service and what operation that service provides. The same way, OpenAPI Initiative (OAI) is trying to standardize the APIs.
Details about this: - https://www.openapis.org/about
Tools
- Visual Studio 2017 with ASP.NET Core 2.1
- AspNetCore package from NuGet
Topic
- Creating ASP.NET Core Application
- Adding NSwag.AspNetCore from NuGet
- Configuration of NSwag in the startup file
- Running Application
- Downloading Nswag Studio
- Swagger specification
- Adding Console app
- Using Generated Client to call Web API
- Making Change in a Startup project
- Run Application to access API using Generated Client
- NSwag example with ASP.NET Core via API Explorer
- Adding class to DemoClient2 to console project.
- Run Application to access API using generated client with ASP.NET Core via API Explorer
Creating Asp.net core Application
Adding NSwag.AspNetCore from NuGet
After adding the NSwag.AspNetCore package, the project structure will look something like the below image.
Next, we need to configure the NSwag Services and Middleware in Startup.cs file.
Configuration of NSwag Services and Middleware in the startup file
In this part, we are going to configure services first in ConfigureServices method by adding the AddSwaggerDocument method.
Code snippet of Startup.cs
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- // add services required for Swagger 2.0 generation
- services.AddSwaggerDocument();
- }
After registering the service, next, let us add NSwag middleware in the Configure Method.
Adding UseSwagger and UseSwaggerUi3 Middleware to Configure Method.
Code Snippet of Startup.cs
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseMvc();
- // Middleware
- if (env.IsDevelopment())
- {
- app.UseSwagger();
- app.UseSwaggerUi3();
- }
- }
Now, we are going to save and run the application.
If you are thinking "we have not added any API, how will it generate documentation for it?", then let me tell you that we have default API created with the name ValuesController.
Let’s run this application to check it.
Running Application
After running the application, it will execute the values using API GET method, as shown below.
Now, to see the documentation part of API, we need to enter the keyword “swagger” to the URL, as shown below.
After entering the URL, you will see the documentation of all HTTP methods (GET, POST, PUT, DELETE) of the API. If you want to share documentation with another team of APIs, you just need to share this URL of Swagger; that’s it.
You can also customize it by adding a description to it.
After generating the Swagger document, now we are going to generate the client code for accessing this API.
For doing that, first, we are going to download NSwag Studio.
Downloading NSwag Studio
To generate the client code for accessing this API with C# console application, here is the link to download: - https://github.com/RSuter/NSwag/wiki/NSwagStudio
After downloading, just install it on your PC. Since Visual Studio does not have support for it, we are using the NSwag Studio to generate the Client Code. Below is the view of NSwag Studio which we have downloaded.
After you see the view of NSwag studio let’s start generating Client code. There are various ways you can do it; let’s first start with “Swagger Specification”
- Swagger Specification
- Net core via API Explorer
- .Net Assembly
- Json Schema
The first demo we are going to work with is “Swagger Specification”
Swagger Specification
In Swagger Specification part, we need to provide “swagger.json” file path which we get from “swagger UI” using that swagger.json file NSwag is going to generate Client code.
Sample path: - http://localhost:53846/swagger/v1/swagger.json
Next, we are going to provide the swagger.json file path to Swagger Specification URL in NSwag Studio, as shown below.
After entering path just click on Create Local copy button to get json from it. And in the below Swagger Specification Json text area, you will able to see json which is download.
Next step after getting json is to Generate outputs and generate files. We are first going to generate Csharp client, and for doing that we are going to choose “Csharp client” checkbox.
After clicking on Csharp Client you can see various settings which you can configure to generate client class. We are not going to make one change in this setting, we are just going to uncheck “Inject Http Client via Constructor (life cycle is managed by caller)” because we are not going to inject HTTP client while accessing it.

Mich DrexlerPosted May 18, 2021, 1:05 PM
Does anybody know how to generate an Angular client where a generic C# Type IEnumerable will be generated to an Typescript Array? Currently my C# IEnumerable type will be generated to just an object :(
Jo FranPosted Jan 26, 2019, 2:43 AM
Thank you for this great article. I'm trying to find a way to limit code generation to a certain set of controllers. For example, the TypeScript-Client for my app shouldn't include all my admin-controllers and respective types. This was possible with the old deprecated reflection method, but I couldn't figure out how to do it with API Explorer. Any ideas?
Mithun PattankarPosted Jan 23, 2019, 2:01 AM
At present I am using this in project, NSwag is really awesome. Extensively written !!
Arvind SinghPosted Jan 22, 2019, 11:23 PM
Nice article.. well explained....
Hadshana KamalanathanPosted Jan 22, 2019, 11:10 PM
Nice one. Well written
Hiten PandyaPosted Jan 22, 2019, 10:58 PM
Great article sir. Thank you so much for sharing.
Sarathlal SaseendranPosted Jan 22, 2019, 12:42 PM
Nicely written article Sai. Thanks for sharing!