Header Ads

Header ADS

Implement Swagger In ASP.Net Core Web API

 


Hello friends, welcome to you all once again on your own website.In this tutorial, we will discuss how to implement Swagger in ASP.Net Core Web API. So if you want to learn from basic to advance. then today all of you should read this post carefully because today I am going to explain full information about this topic in full details to all of you.


What is Swagger?

The Swagger is a set of rules which provides the users functionality to understand the structure of the REST API, and it can also be used to share documentation among product managers, testers, and developers, but can also be used by various tools to automate API related processes.

There are many such frameworks around but Swagger comes with many benefits.

  • Swagger is understandable by technical as well as non-technical users. Because of its friendly UI, it can be understandable by project managers, BA’s, & even the clients.
  • Testing and debugging APIs gets easier.
  • Readable by both human and machine, you can easily use it to automate the API process.

APIs that use Swagger are easy to understand, modify, and consume. Everything gets clear and that is the reason why big companies are using it in their processes.

Testing Web API’s is a challenge. It has a dependency on various third-party tools, requires installing different packages,  and it can get all messed up. Swagger can make it easy and quick.

How to Implement Swagger


Step 1 - Open the Visual Studio 2019 and Select a new project.



Step 2 - Select the Project Type Asp.net Core Web API , and click on next.



Step 3 - give the title of the Project and then Click on the Next.



Step 4 - Now in this step Select your framework, I am using the framework (.NET Core 3.1) and Click on Create.



Now, we are going to add a package where you can use the NuGet Package Manager or Package Manage Console. 

  • For Package Manager, just go to Tools→ NuGet Package Manager → NuGet Package Manager or you can right click on Dependencies and select nuget package manage.   
  • in browse tab, search Swashbuckle. AspNetCore and install it.


So, go to the Startup.cs file.

Add namespace using Microsoft.OpenApi.Models;

Add the following code to the ConfigureServices Method.


            services.AddSwaggerGen();
     services.AddSwaggerGen(c =>
     {
       //Swashbuckle.AspNetCore
        c.SwaggerDoc("v1", new OpenApiInfo
        {
          Version = "v1",
          Title = "Implement Swagger UI",
          Description = "A dotnet Blue Ocean example to Implement Swagger UI",
          TermsOfService = new Uri("https://dotnetblueocean.blogspot.com/"),
          Contact = new OpenApiContact
          {
            Name = "DotNet Office",
            Email = "Email Address",
            Url = new Uri("https://www.youtube.com/dotnetblueocean"),
          },
          License = new OpenApiLicense
          {
            Name = "License Information",
            Url = new Uri("https://www.youtube.com/dotnetblueocean"),
          }
        });
      });



Add the following code to the Configure Method of the Startup.cs file.

            app.UseSwagger();
     app.UseSwaggerUI(c =>
     {
       c.SwaggerEndpoint("/swagger/v1/swagger.json", "Showing API V1");
     });


before run the prohect, you will do some chane in launchSetting.json like the following image.




Finally, after running the project you will get something like the following image.



Today I told you all through this post about how you can implement Swagger in ASP.Net Core Web API. if you all like this post. then definitely share it with your friends. If there is any confusion. then all of you will tell me in the comments below. I will surely answer each and every comment.

No comments

Theme images by DNY59. Powered by Blogger.