• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Friday, April 17, 2020

How to use Swagger in Web API Application

 Admin     April 17, 2020     .Net, Web API     No comments   

In this article, I am going to discuss how to use Swagger in WEB API Application to document and test restful Web API services. Please read my previous article where we discussed How to Create ASP.NET Web API Application step by step before proceeding to this article as we are going to work with the same example.

What is Swagger?
The Swagger is a simple but powerful representation of the RESTful API. Nowadays most of the developers are using Swagger in almost every modern programming language and deployment environment to document. With a Swagger-enabled Web API, you will get interactive documentation, client SDK generation as well as discoverability.

How to Add Swagger to Web API Project?
To add Swagger to your ASP.NET Web API project, you need to install an open-source project called Swashbuckle via NuGet as shown below.
Install Swashbuckle package for Swagger
Install Swashbuckle package
Once the package is installed successfully, navigate to the App_Start folder in the Solution Explorer. You will find a new file called SwaggerConfig.cs. This is the file where Swagger is enabled and any configuration options should be set here.
SwaggerConfig.cs in Web API Architecture
SwaggerConfig.cs
How to Configure Swagger in ASP.NET Web API Application?
To enable Swagger and Swagger UI, modify the SwaggerConfig class as shown below
    namespace FirstWebAPIDemo
    {
        public class SwaggerConfig
        {
            public static void Register()
            {
                var thisAssembly = typeof(SwaggerConfig).Assembly;
                GlobalConfiguration.Configuration
                  .EnableSwagger(c => c.SingleApiVersion("v1", "First WEB API Demo"))
                  .EnableSwaggerUi(); 
            }
        }
    }
Start a new debugging session by pressing the F5 key and navigate to http://localhost:[PORT_NUM]/swagger and then you should see the help pages for your APIs.
Swagger UI in Web API
Swagger UI
Ok. That’s cool. Now expand an API and then click on the “Try it out!” button which will make a call to that specific API and return results as shown in the below image.
Calling API using Swagger in Web API
Calling our API
Here click on the Try it out Button which will display the result as shown below.
API Resposne with Swagger in Web API
API Response
In the same way, you can test all other methods.

How to enable Swagger to use XML Comments in ASP.NET Web API Application?
As of now, we use the minimum configuration to get started. But now we are going to add more customization. We can tell the Swashbuckle to use our custom XML comments to add more details about our APIs to the Swagger metadata.

First, we need to enable XML documentation file creation during the build. In the Solution Explorer right-click on the Web API project and click on the Properties. Click the Build tab and navigate to Output. Make sure the XML documentation file is checked. You can leave the default file path. In our case its bin\FirstWebAPIDemo.XML as shown below
Enable Swagger to use XML comments in Web API
Enable Swagger to use XML comments
Next, we need to tell the Swashbuckle to include our XML comments in the Swagger metadata. To do this we need to add the following line to SwaggerConfig.cs. Make sure to change the file path to the path of your XML documentation file.
c.IncludeXmlComments(string.Format(@”{0}\bin\FirstWebAPIDemo.XML”, System.AppDomain.CurrentDomain.BaseDirectory));
Configuration, so far
    namespace FirstWebAPIDemo
    {
        public class SwaggerConfig
        {
            public static void Register()
            {
                var thisAssembly = typeof(SwaggerConfig).Assembly;
                GlobalConfiguration.Configuration
                  .EnableSwagger(c =>
                  {
                      c.SingleApiVersion("v1", "First WEB API Demo");
                      c.IncludeXmlComments(string.Format(@"{0}\bin\FirstWebAPIDemo.XML",
                                           System.AppDomain.CurrentDomain.BaseDirectory));
                  })
                  .EnableSwaggerUi();
            }
        }
    }
Let’s add some XML documents to our API methods as shown below. Here we are adding XML Document to the get method. Modify the Get method as shown below.
    /// 
    /// Get All the Values
    /// 
    /// 
    /// Get All the String Values
    /// 
    /// 
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
Run the application and navigate back to /swagger. You should see more details added to your API documentation as shown below.
API comments are shown in Swagger
API comments are shown
Summary:
I Hope this post will be helpful to integrate Swagger with your Web API Application.
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape