Search code examples
swaggerasp.net-core-webapi.net-8.0

Cannot load Swagger page from new API locally


I'm using Visual Studio 2022, I have a new ASP.NET Core 8.0 Web API project. I removed all traces of the example weather forecast that was loaded when I created the project, but now when I debug my application, I get the following error:

This localhost page can't be found.
No web page was found for the web address: https://localhost:7088/swagger

I also manually adjusted the URL but still get the error

No web page was found for the web address: https://localhost:7088/api/present

From what I can see, this setup is caused by incorrect settings in either the controller class, launchsettings.json or program.cs.

My controller code looks like this:

Controller class
[ApiController]
Route("api/[controller]")]
public class PresentController(IPresentsService _presentsService) : ControllerBase
{
    [HttpGet]
    [Produces(MediaTypeNames.Application.Json)]
    [ProducesResponseType(typeof(IEnumerable<PresentDto>), StatusCodes.Status200OK)]
    public async Task<IEnumerable<PresentDto>> GetAllPresentsAsync()
    {
        return await _presentsService.GetAllPresentsAsync();
    }
}

My launchSettings.json looks like this:

"https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7088;http://localhost:5030",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
}

My Program.cs looks like this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

// Add services to the container.
builder.Services.AddScoped<IPresentsRepository, PresentsRepository>();
builder.Services.AddScoped<IPresentsService, PresentsService>();

builder.Services.AddDbContext<PresentsDbContext>(options =>
{
    options.UseNpgsql(builder.Configuration.GetConnectionString("PresentsDb"));
});

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseHttpsRedirection();

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    c.RoutePrefix = "";
});

app.Run();

Can somebody please explain what I am doing wrong? I have tried a few variations with the likes of Swashbuckle too, SwaggerUI etc but I have not had any luck.


Solution

  • This localhost page can't be found. No web page was found for the web address: https://localhost:7088/swagger

    The reason why you face the error is you set a RoutePrefix to the swagger path, but you still use the old one, you should remove the c.RoutePrefix = "";, then the path(https://localhost:7088/swagger) is right.

    No web page was found for the web address: https://localhost:7088/api/present

    Besides, inside the your program.cs, you removed the app.UseSwagger() and app.MapControllers(); , this will cause your second error, there is no route for the api controller and the swagger middelware will not generate the swagger json .

    The right codes should as below:

    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    app.UseHttpsRedirection();
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    
    });
    app.MapControllers();
    app.Run();
    

    Thanks for mention it, I forget to see your service, you are right, you need also add the related service like below:

    builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
    
    builder.Services.AddSwaggerGen();
    
    var app = builder.Build();