Tuesday, August 3, 2021

Adding Swagger to ASP.NET WEB API Project

 This is a very simple activity

1. Add Swashbuckle.AspNetCore package from nuget. 

2. Add following config in startup.cs

        public void ConfigureServices(IServiceCollection services)

        {

            services.AddControllers();

            services.AddSwaggerGen(options =>

            {

                options.SwaggerDoc("v2", new Microsoft.OpenApi.Models.OpenApiInfo

                {

                    Title = "XXXX API",

                    Version = "1.0.0",

                    Description = "This is XXXX API",

                });

            });

        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

        {

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>

            {

                endpoints.MapControllers();

            });

            app.UseSwagger();

            app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v2/swagger.json", "XXXX API"));

        }

3. Run the project and simply browse to  /swagger endpoint . 
e.g. https://localhost:44350/swagger (this will automatically take you to https://localhost:44350/swagger/index.html)

No comments:

Post a Comment

 using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; using System.Xml.XPath; //<table class="common-table medium js-table js-stre...