find and solve || findandsolve.com
Please wait.....

How to integrate Swagger UI in a .NET Core Web API

How to integrate Swagger UI in a .NET Core Web API

What is Swagger / OpenAPI?

In this article, we are going to implement and learn how to intrgrate Swagger UI in an ASP.NET Core Web API application. Swagger is a language-agnostic specification for describing REST APIs and This is also referred to as OpenAPI.It allows both computers and humans to understand the capabilities of a service without any direct access to the implementation (source code, network access, documentation). 

click here to download source code

Open your main project and install  Swashbuckle.AspNetCore  by Manage Nuget Packages as give below




Note:- Add new  SwaggerExample.xml in  main project  and past this code as give below

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>SwaggerExample</name>
    </assembly>
    <members>
    </members>
</doc>

Or
Add SwaggerExample.xml from menually as given image

1.Right click on your main project 
2.Click on Properties
3.Select Build 
4.Check XML documentation file
as give below


Add New Class SwaggerSetup.cs and past this code

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SwaggerExample.Config
{
    public static class SwaggerSetup
    {
        public static void AddSwaggerSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            services.AddSwaggerGen(c =>
            {
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo
                    {
                        Version = "v1",
                        Title = "ToDo API",
                        Description = "Swagger example in asp.net core web api",
                        TermsOfService = new Uri("https://findandsolve.com/how-to-implemenation-swagger-in-asp-net-core"),
                        Contact = new OpenApiContact
                        {
                            Name = "Find and solver",
                            Email = string.Empty,
                            Url = new Uri("https://twitter.com/spboyer"),
                        },
                        License = new OpenApiLicense
                        {
                            Name = "Use under LICX",
                            Url = new Uri("https://example.com/license"),
                        }
                    });
                   
                });
                var filePath = Path.Combine(System.AppContext.BaseDirectory, "SwaggerExample.xml");
               //SwaggerExample.xml is th xml file which is added in our main page as given above
               
            });
        }
        public static void UseSwaggerSetup(this IApplicationBuilder app)
        {
            if (app == null) throw new ArgumentNullException(nameof(app));
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger Demo Project");
            });
        }
    }
}


After that Edit  Startup.cs project

  public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        //Call this function here
        services.AddSwaggerSetup();//We are already created this function in  SwaggerSetup.cs  
    }

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
       if (env.IsDevelopment())
        {
          app.UseDeveloperExceptionPage();
        }
       //Call this function here
        app.UseSwaggerSetup();
       app.UseHttpsRedirection();
       app.UseRouting();
       app.UseAuthorization();
       app.UseEndpoints(endpoints =>
        {
                endpoints.MapControllers();
            });
        }

Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response