Reputation: 874
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
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?
Upvotes: 2
Views: 453
Reputation: 21838
My Swashbuckle.AspNetCore
library version is 5.6.3
.
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
If don't this settings, the url should be:
https://localhost:44355/swagger
Upvotes: 1