Tuesday, July 23, 2019

Setup Swagger for ASP.NET WebAPI documentation

this blog demonstrates step by step to add swagger in web api project and will submit  http request GET/POST/PUT via swagger UI.

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 Sweggar 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");

                    });
        }
    }

build and run webAPI project and browser http://localhost:65096/swagger

Swagger UI documentation















Swagger allow you can submit HTTP request GET, HTTP POST, HTTP PUT against Web API

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

No comments:

Post a Comment