Wednesday, February 28, 2018

How to use Swagger in Web API

In this blog, we will discuss about web api documentation and will demonstrate basic steps how to setup swagger in web api project and also show how to test web api by submitting http request GET/POST/PUT via swagger UI.

Please read my previous blog when we discussed how to create a web api in ASP.NET MVC Application and describe basic steps to create an asp.net web api.

Swagger is API developer tools for the Open API Specification (OAS), enabling development across the entire API life cycle, from design and documentation, to test and deployment.

Definition -
Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.”


To add Swagger into WEBAPI Project, you need to perform only 2 steps

1.   Install SwashBuclkle open source project via Nuget.


Install SwashBuclkle Nuget Package

2.   Configure Swagger Config File :

 After package installed, you will find a new file ‘SwaggerConfig.cs’ under App_Start Folder

    
Swagger Config

public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {             
                        c.SingleApiVersion("v1""DemoWebAPI");                 
                    })
                .EnableSwaggerUi(c =>
                    {
                        c.DocumentTitle("Demo Web API");

                    });
        }

    }
 
It is done J 

now build the web api project and run it.

Browser http://localhost:65096/swagger

Swagger UI documentation















Swagger allows you to submit HTTP request GET, HTTP POST, HTTP PUT against Web API

here is an an example 

Submit HTTP GET Request For /api/Projects/{projectId}



Swagger UI GET


Other swagger related posts:



·         Disable Swagger UI



Thanks for visiting!!

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...