Home > Enterprise >  Getting error for Swagger UI with an ASP.NET Core 6 Web API with custom routing
Getting error for Swagger UI with an ASP.NET Core 6 Web API with custom routing

Time:01-19

I have custom routing for my ASP.NET Core 6 Web API:

[ApiController]
[Route("ForecastService")]
public class WeatherForecastController : ControllerBase
{
    [HttpGet("forecast")]
    public IEnumerable<string> Get1()
    {
        return new[] { "forecast" };
    }
}

I have made this change in the Swagger configuration:

 builder.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();

        var app = builder.Build();

        // Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            //app.UseSwagger();
            //app.UseSwaggerUI();
            app.UseSwagger(c => c.RouteTemplate = "ForecastService/{documentName}/swagger.json");
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("ForecastService/v1/swagger.json", "Forcast API");
                c.RoutePrefix = "ForecastService/swagger";
            });
        }

Now while trying to run browse to http://localhost:5113/ForecastService/swagger/index.html, I am getting an error:

Not found http://localhost:5113/ForecastService/swagger/ForecastService/v1/swagger.json

What am I missing here?

enter image description here

CodePudding user response:

Change your configuration for swagger like below:

//miss the swagger after ForecastService.....
app.UseSwagger(c => c.RouteTemplate = "ForecastService/swagger/{documentName}/swagger.json");
app.UseSwaggerUI(c =>
{
    //miss the `/` in the beginning and also miss the swagger after ForecastService...
    c.SwaggerEndpoint("/ForecastService/swagger/v1/swagger.json", "Forcast API");
    c.RoutePrefix = "ForecastService/swagger";
});

CodePudding user response:

modify below lines as

    app.UseSwagger();
    app.UseSwaggerUI();

Use URL as

http://localhost:5113/swagger
  • Related