Skip to main content

Swagger UI in ASP.NET Web API


What is SWAGGER UI?

Swagger UI is an HTML/CSS/JS framework that provides interactive documentation of your RESTful APIs and generates a navigable UI of the documentation. Swagger UI provides automatically generated HTML assets that give you automatic documentation and even an online test tool.

It is extremely easy to set up, and comes with a lot of configurable options like XML Documentation, various kinds of Authentications (Basic, OAUTH2, API key), etc. which can be configured depending on the security schemes set up in your APIs.

This can reduce the time and efforts you spend on creating your Client Application to consume and test your RESTful APIs. With minimum amount of code, you will get an easy to use test client for your APIs.

Benefits of using SWAGGER UI?

  • A Sandbox!
  • Create test client for your APIs in seconds!
  • It's comprehensible for developers and non-developers.
  • It's easily adjustable.
  • It’s like a sitemap for your API.

How to set Up the Swagger UI in your WEB APIs?


  • Install the Swagger Nuget Package in your API project. This will add the Swashbuckle.Core.dll assembly to your project, and other required files that Swagger UI might need.
  • Run your API project, and append “/Swagger” to the default API URL.


  • This will open the Swagger UI page which will have a list of all your API controllers, and their respective methods.

  • You can hit the “Try it out!” button to test the API method. This will fire that API method, and will list out the Response URL, Response Body, Response Headers in the same section as displayed below:
  • If your API has some security schemes like BasicAuth, API Key or OAuth2, then on the Swagger UI page, you can notice the Red Exclamation symbol next to some of the method names, this indicated that those API methods are protected, and needs to be authenticated before consuming them.

  • Please note that if you try to test the Authenticated API method without providing proper authentication, it will return Unauthorized Response Code (401), along with the error message as shown in image below:
  • If you still want to go ahead and consume Swagger to authenticate and test your protected APIs, all you need to do is to configure the SwaggerConfig.cs file provided by Swagger. That’s it!! 
Here is a helpful link to do so:
https://blogs.msdn.microsoft.com/pratushb/2016/04/28/enable-swagger-to-authenticate-against-azure-ad/ 

Comments

  1. Accord IT Training is providing Best Java Training Institute in Chennai with placement.
    for any queries
    contact on +91 9940289059
    #Java Institute
    #Java Training in chennai
    #Core Java Training in Chennai

    ReplyDelete
  2. Appreciating the persistence you put into your blog and detailed information you provide.Thanks for your blog
    Azure training chennai | Azure training course chennai

    ReplyDelete

  3. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.

    UIpath training in chennai | uipath training class chennai

    ReplyDelete
  4. You got a really useful blog here. I gained more knowledge regarding azure cloud migration services. I got amazing information from this blog.
    azure cloud migration services

    ReplyDelete
  5. Information shared above is highly appreciated. I found the blog very informative. Your article has inspired many of us to learn this. You have shared your deep knowledge on Swagger. Recently i came to know MaxMunus offers the best Swagger training across the globe. If you come across anyone looking for training, you can ask him to contact them directly. They are reachable at:-
    Swagger Training

    ReplyDelete

Post a Comment

Popular posts from this blog

Handling Concurrency in Azure Functions (HTTP Triggered)

Concurrency & Isolation Concurrency is one of the most common requirements when we don't want the resource to be accessed by multiple requests at the same time. Lets say we have a Database that holds products stock information, then it is very important that any read/write operation that happens on the Database must hold the property of Isolation . The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict – as opposed to relaxed – serializability), the effects of an incomplete transaction might not even be visible to another transaction. Options Available in Azure Functions Recently while working on Azure Functions, I discovered a really simple way of handling Concurrency via code. There are ways to con

Azure Functions - Retrieve data from Dynamics 365 CRM/CE Online using CRM OData Web API

Introduction: This blog describes how you can retrieve data from Dynamics 365 CRM/CE Online in an Azure Function, using CRM OData Web API. The CRM Web API has a couple of advantages over the traditional SOAP based approach to interact with CRM. These are rest based services, its easy to use and transport over http protocol , and there is no need to use additional web resources (XrmServiceToolkit ) of SDK (SDK.REST). Here is a detailed documentation on CRM OData Wbeb API. I will be using an Azure Function Application in Visual Studio 2017 to retrieve records from Dynamics 365 CRM/CE, by making use of OData API Endpoint provided by CRM. Prerequisites: Dynamics 365 CRM Online Trial https://trials.dynamics.com/ https://signup.microsoft.com/Signup?OfferId=bd569279-37f5-4f5c-99d0-425873bb9a4b&dl=DYN365_ENTERPRISE_PLAN1&Culture=en-us&Country=us&ali=1 Azure Subscription related to CRM Online instance https://portal.azure.com/ Visual Studio 2017 with Cl

Retrieve 5000+ Records in Dynamics 365 CE using CRM OData, Fetch XML and C#

If you want to retrieve 5000+ records in Dynamics 365 CE, you need to make use of Paging concept of CRM. By default 5000 records are retrieved in a single OData call in CRM. To get more, you need to make subsequent calls to OData Endpoint by passing a paging cookie in the request. I will be using a simple C# Console Application to retrieve 5000+ records from Dynamics 365 CE, by making use of OData API Endpoint provided by CRM and passing Fetch XML in OData Call . Concept: CRM OData Endpoints allows us to use Fetch XML to retrieve data from Microsoft Dynamics 365 CE.  If your resultset has more than 5000 records, you will be returned with first 5000 records ONLY in one OData call. To know if the resultset has more than 5000 records, we make use of the response from OData call. The response is added with a cookie value contained in a key - "@Microsoft.Dynamics.CRM.fetchxmlpagingcookie" If you wish to fetch the next set of resultset data, you will have to pass this c