Marcel James
Marcel James

Reputation: 874

Cant generate Swagger .JSON Doc from .NET Core API

I watched like 4 videos and followed some guides on Internet, about how to generate Swagger .JSON Doc from .NET Core API.

They all tell me to do the same things: Add some comands in my Startup.cs class, as the image below enter image description here

But, when I execute my app and try to navegate to it, it simple doesnt open anything. I tried alot of URLs on browser:

https://localhost:44305/swagger/index.html

https://localhost:44305/swagger

https://localhost:44305/swagger/v1/swagger.json

and nothing happens! What sould I try?

enter image description here

Upvotes: 2

Views: 453

Answers (1)

Jason Pan
Jason Pan

Reputation: 21838

My Swashbuckle.AspNetCore library version is 5.6.3.

enter image description here

Test Code in my local, and it works for me.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace webapi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "webapi", Version = "v1" });
            });
        }

        // 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.UseSwagger();
                app.UseSwaggerUI(c => {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "webapi v1");
                    //c.RoutePrefix = string.Empty;
                }) ;
            }
            else
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "webapi v1");
                    //c.RoutePrefix = string.Empty;
                });
            }

            app.UseHttpsRedirection();
        
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

Test Result

If enable c.RoutePrefix = string.Empty, the url should be like :

https://localhost:44355

enter image description here

If don't this settings, the url should be:

https://localhost:44355/swagger

enter image description here

Upvotes: 1

Related Questions