Wednesday, October 11, 2023

Swagger in WEB API Application

 Let us take the previous example itself.,

Please refer :

https://dotnetbyudayrajakonda.blogspot.com/2023/10/creating-basic-aspnet-web-api.html

First, we need to install a package called

 Swashbuckle via NuGet as shown below.








You can install NuGet packages in two ways one using command  and the other using manage Nuget package from project window










Select it and click on Install








Click on Ok 

To check if that package is installed or not. You will find that in the App Start folder as shown below






Now, we will configure Swagger.

First step goto SwaggerConfig.cs and delete the commented code and modify the code as below



 
Now, Run you will be able to see the Swagger UI


In the url , type the name Swagger , then you will be able to see.














Swagger with XML Comments


Now, 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





Next, goto  SwaggerConfig.cs and make the below changes

public class SwaggerConfig
    {
        public static void Register()
        {
            GlobalConfiguration.Configuration
               .EnableSwagger(c =>
               {
                   c.SingleApiVersion("v1", "First WEB Swagger Demo");
                   c.IncludeXmlComments(string.Format(@"{0}\bin\WebApplication1.XML",
                                        System.AppDomain.CurrentDomain.BaseDirectory));
               })
               .EnableSwaggerUi();
        }
    }


Now, goto ValuesController and add some xml comments to it as shown below











Now, run the application and you will be able to see the xml comments 





No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...